Hi,
<div><br></div><div>I'm experimenting with Erlang process and found something that not expected. </div><div><br></div><div>What I do is creating 1M Erlang processes and measure memory usage. Code is as follow:</div><div>
<div>-module(generator).</div><div>-export([start/0, generator/2, loop/0]).</div><div><br></div><div>start() -></div><div> Pid = self(),</div><div> spawn( fun()->generator(19,Pid) end ),</div><div> count (1).</div>
<div><br></div><div>count (Num) -></div><div> receive</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>add -></div><div>%<span class="Apple-tab-span" style="white-space:pre"> </span> io:format("Oopss, ~n"),</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> if </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>0 =:= ( Num rem 10000 ) -> io:format("Count ~p~n", [Num]);</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>true -> Q = 3</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> end,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> count (Num + 1);</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>_ -></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> io:format("haha, ~n")</div><div> after 30000 -></div><div>
<span class="Apple-tab-span" style="white-space:pre"> </span> io:format("Count ~p~n", [Num]),</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> count (Num)</div><div> end.</div><div>
loop() -></div><div> receive</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>_ -></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> io:format("Oops ~n"),</div>
<div>%<span class="Apple-tab-span" style="white-space:pre"> </span> proc_lib:hibernate(?MODULE, loop(), [])</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> loop()</div><div> end.</div><div>
generator( 0, Pid ) -></div><div> Pid ! add,</div><div>% proc_lib:hibernate(?MODULE, loop(), []);</div><div> loop();</div><div>generator( Depth, Pid ) -></div><div> spawn( fun()->generator(Depth - 1, Pid) end),</div>
<div> spawn( fun()->generator(Depth - 1, Pid) end),</div><div> Pid ! add,</div><div>% io:format("Depth: ~p, pid: ~p~n", [Depth, Pid]),</div><div>% proc_lib:hibernate(?MODULE, loop(), []).</div><div>
loop().</div></div><div>===============End of Code=============</div><div><div>2> i(0,58,0).</div><div>[{current_function,{generator,loop,0}},</div><div> {initial_call,{erlang,apply,2}},</div><div> {status,waiting},</div>
<div> {message_queue_len,0},</div><div> {messages,[]},</div><div> {links,[]},</div><div> {dictionary,[]},</div><div> {trap_exit,false},</div><div> {error_handler,error_handler},</div><div> {priority,normal},</div><div> {group_leader,<0.25.0>},</div>
<div> {total_heap_size,233},</div><div> {heap_size,233},</div><div> {stack_size,1},</div><div> {reductions,9},</div><div> {garbage_collection,[{min_bin_vheap_size,46368},</div><div> {min_heap_size,233},</div>
<div> {fullsweep_after,65535},</div><div> {minor_gcs,0}]},</div><div> {suspending,[]}]</div></div><div><div><br></div><div>3> memory().</div><div>[{total,2759895784},</div><div>
{processes,2751081712},</div><div> {processes_used,2751067504},</div><div> {system,8814072},</div><div> {atom,615121},</div><div> {atom_used,589205},</div><div> {binary,8608},</div><div> {code,5845747},</div><div> {ets,270928}]</div>
</div><div><br></div><div>Total memory usage is about 2.7G. While when I use i() and i(pid) I saw that each process only uses about 233 bytes. I can't find where the memory is used. Any idea?</div><div><br></div><div>
Thanks</div><div>Andy</div>