[erlang-questions] message ring test (memory allocation question)

Matthias Lang matthias@REDACTED
Thu Mar 6 10:09:59 CET 2008


Hi,

Nobody else has answered this already as far as I can see, maybe
because you've figured out the answer yourself.

Death through memory exhaustion is expected in the first case, and you
imply that you weren't suprised either (the list L is going to eat
400k on most machines. 32000 copies of that is too big for my machine)

Binaries are indeed treated differently by the implementation. They're
stored in their own reference-counted memory pool, which is possible
since binaries by definition can't contain references to anything
else. Each of the 32000 message queues then contains just a pointer
(more or less) to the binary, as you guessed.

Matt

----------------------------------------------------------------------

Allen McPherson writes:
 > Hello,
 > 
 > First post here from a new Erlang programmer.  As an exercise I
 > have written the message ring benchmark that Joe talked about in
 > his book:
 > 
 > -module(cleanring).
 > -compile(export_all).
 > 
 > start(N, Msg) ->
 >      StartPid = launch(self(), N),
 >      StartPid ! {Msg, self(), 0},
 >      receive
 > 	{MsgRecvd, _FromPid, _Hop} -> done
 >      end,
 >      StartPid ! die,
 >      MsgRecvd.
 > 
 > launch(Pid, N) ->
 >      FromPid = spawn(fun() -> loop(Pid) end),
 >      if
 > 	N =:= 1 -> FromPid;
 > 	true    -> launch(FromPid, N-1)
 >      end.
 > 
 > loop(ToPid) ->
 >      receive
 > 	{Term, _FromPid, Hop} ->
 > 	    ToPid ! {Term, self(), Hop+1},
 > 	    loop(ToPid);
 > 	die ->
 > 	    ToPid ! die
 >      end.
 > 
 > 
 > If I run as follows, it dies in malloc:
 > 
 > 1> c(cleanring).
 > 2> L = lists:seq(1,100000).
 > 3> cleanring:start(32000, L).
 > 
 > If I make L a binary (in a restarted erl shell) it runs fine (and too  
 > fast to believe):
 > 
 > 1> c(cleanring).
 > 2> L = lists:seq(1,100000).
 > 3> LL = term_to_binary(L).
 > 4> cleanring:start(32000, LL).
 > 
 > Is the runtime somehow just passing around a pointer to LL by virtue
 > of it being a binary?  I'm not sure why it doesn't crash as the first
 > run did.
 > 
 > Also, if you spot anything really wrong with the program I'd  
 > appreciate a
 > heads up.  I'm new to Erlang and am sometimes not sure if the code I  
 > write
 > is in the "right mindset" (functional programming-wise).
 > 
 > Thanks
 > --
 > Al
 > 
 > _______________________________________________
 > erlang-questions mailing list
 > erlang-questions@REDACTED
 > http://www.erlang.org/mailman/listinfo/erlang-questions
 > 



More information about the erlang-questions mailing list