Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much memory used in Erlang VM?

Tags:

erlang

i want to konw this data struct will use how much memory in Erlang VM?

[{"3GPP-UTRAN-FDD", [{"utran-cell-id-3gpp","CID1000"}, "1996-12-19t16%3a39%3a57-08%3a00", "1996-12-19t15%3a39%3a27%2e20-08%3a00"]}]

In my application, every process will store this data in self's loop data, and the numbert of this proces will be 120000.

The result which i test:

don't store this data, the memory will be:

memory[kB]:  proc 1922806, atom    2138, bin   24890, code   72757, ets  459321

store this data, the momory will be:

memory[kB]:  proc 1684032, atom    2138, bin   24102, code   72757, ets  459080

So the big difference is the memoery used by proc: (1922806 - 1684032) / 1024 = 233M.

After research, i find an insterting thing:

L = [{"3GPP-UTRAN-FDD", [{"utran-cell-id-3gpp","CID1000"}, "1996-12-19t16%3a39%3a57-08%3a00", "1996-12-19t15%3a39%3a27%2e20-08%3a00"]}].
B = list_to_binary(io_lib:format("~p", L)).   
erts_debug:size(B). % The output is 6

The memory just use 6 words after using binary? How to explain this?

like image 965
BlackMamba Avatar asked Oct 22 '25 05:10

BlackMamba


2 Answers

There are two useful functions for measuring the size of an Erlang term: erts_debug:size/1 and erts_debug:flat_size/1. Both of these functions return the size of the term in words.

erts_debug:flat_size/1 gives you the total size of a message without term-sharing optimization. This is guaranteed to be the size of the term if it is copied to a new heap, as with message passing and ets tables.

erts_debug:size/1 gives you the size of the term as it is in the current process' heap, allowing for memory usage optimization by sharing repeated terms.

Here is a demonstration of the differences:

1> MyTerm = {atom, <<"binary">>, 1}.
{atom,<<"binary">>,1}
2> MyList = [ MyTerm || _ <- lists:seq(1, 100) ].
[{atom,<<"binary">>,1}|...]
3> erts_debug:size(MyList).
210
4> erts_debug:flat_size(MyList).
1200

As you can see, there is a significant difference in the sizes due to term sharing.

As for your specific term, I used the Erlang shell (R16B03) and measured the term with flat_size. According to this, the memory usage of your term is: 226 words (1808B, 1.77KB).

This is a lot of memory to use for what appears to be a simple term, but that is outside of the scope of this question.

like image 136
Soup d'Campbells Avatar answered Oct 23 '25 23:10

Soup d'Campbells


the size of the whole binary is 135 bytes when you do it list_to_binary(io_lib:format("~p", L))., if you are on a 64 bit system it represents 4.375 words so 6 words should be the correct size, but you have lost the direct access to the internal structure.

Strange but can be understood:

19> erts_debug:flat_size(list_to_binary([random:uniform(26) + $a - 1 || _ <- lists:seq(1,1000)])).                                                                                  
6                                                                                                                                                                                   
20> erts_debug:flat_size(list_to_binary([random:uniform(26) + $a - 1 || _ <- lists:seq(1,10000)])).                                                                                 
6                                                                                                                                                                                   
21> size(list_to_binary([random:uniform(26) + $a - 1 || _ <- lists:seq(1,10000)])).                                                                                                 
10000                                                                                                                                                                               
22> (list_to_binary([random:uniform(26) + $a - 1 || _ <- lists:seq(1,10000)])).                                                                                                     
<<"myeyrltgyfnytajecrgtonkdcxlnaoqcsswdnepnmdxfrwnnlbzdaxknqarfyiwewlugrtgjgklblpdkvgpecglxmfrertdfanzukfolpphqvkkwrpmb"...>>                                                       
23> erts_debug:display({list_to_binary([random:uniform(26) + $a - 1 || _ <- lists:seq(1,10000)])}).
{<<10000 bytes>>}
"{<<10000 bytes>>}\n"
24>

This means that the erts_debug:flat_size return the size of the variable (which is roughly a type information, a pointer to the data and its size), but not the size of the binary data itself. The binary data is stored elsewhere and can be shared by different variables.

like image 35
Pascal Avatar answered Oct 24 '25 01:10

Pascal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!