Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The VM is supposed to manage the garbage collecting. If you use a gen_server, or if you use a "home made" server_loop(State), you should have always the same pattern:</p> <pre><code>server_loop(State) -&gt; A = somefunc(State), B = receive mesg1 -&gt; func1(...); ... after Timeout -&gt; func2(...) end, NewState = func3(...), server_loop(NewState). </code></pre> <p>As long as a process is alive, executing this loop, the VM will allocate and manage memory areas to store all needed information (variables, message queue...+ some margin) As far as I know, there is some spare memory allocated to the process, and if the VM does not try to recover the memory very fast after it has been released, but if you force a garbage collecting, using erlang:garbage_collect(Pid) you can verify that the memory is free - see example bellow.</p> <pre><code>startloop() -&gt; spawn(?MODULE,loop,[{lists:seq(1,1000),infinity}]). loop(endloop) -&gt; ok; loop({S,T}) -&gt; NewState = receive biglist -&gt; {lists:seq(1,5000000),T}; {timeout,V} -&gt; {S,V}; sizelist -&gt; io:format("Size of the list = ~p~n",[length(S)]), {S,T}; endloop -&gt; endloop after T -&gt; L = length(S) div 2, {lists:seq(1,L),T} end, loop(NewState). %% Here, NewState is a copy of State or a totally new data, depending on the %% received message. In general, for performance consideration it can be %% interesting to take care of the function used to avoid big copies, %% and allow the compiler optimize the beam code %% [H|Q] rather than Q ++ [H] to add a term to a list for example </code></pre> <p>and the results in the VM:</p> <pre><code>2&gt; P = lattice:startloop(). &lt;0.57.0&gt; ... 6&gt; application:start(sasl). .... ok 7&gt; application:start(os_mon). ... ok ... 11&gt; P ! biglist. biglist ... </code></pre> <p><strong>% get_memory_data() -> {Total,Allocated,Worst}.</strong></p> <pre><code>14&gt; memsup:get_memory_data(). {8109199360,5346488320,{&lt;0.57.0&gt;,80244336}} ... 23&gt; P ! {timeout,1000}. {timeout,1000} 24&gt; memsup:get_memory_data(). {8109199360,5367361536,{&lt;0.57.0&gt;,80244336}} </code></pre> <p><strong>the worst case is the loop process: {&lt;0.57.0>,80244336}</strong></p> <pre><code>... 28&gt; P ! sizelist. Size of the list = 0 sizelist ... 31&gt; P ! {timeout,infinity}. {timeout,infinity} 32&gt; P ! biglist. biglist 33&gt; P ! sizelist. Size of the list = 5000000 sizelist ... 36&gt; P ! {timeout,1000}. {timeout,1000} 37&gt; memsup:get_memory_data(). {8109199360,5314289664,{&lt;0.57.0&gt;,10770968}} </code></pre> <p><strong>%% note the garbage collecting in the previous line: {&lt;0.57.0>,10770968}</strong></p> <pre><code>38&gt; P ! sizelist. sizelist Size of the list = 156250 39&gt; memsup:get_memory_data(). {8109199360,5314289664,{&lt;0.57.0&gt;,10770968}} ... 46&gt; P ! sizelist. Size of the list = 0 sizelist 47&gt; memsup:get_memory_data(). {8109199360,5281882112,{&lt;0.57.0&gt;,10770968}} ... 50&gt; erlang:garbage_collect(P). true 51&gt; memsup:get_memory_data(). {8109199360,5298778112,{&lt;0.51.0&gt;,688728}} </code></pre> <p><strong>%% after GC, the process &lt;0.57.0> is no more the worst case</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload