How much memory is used in Erlang VM?

I want this data structure to use the amount of memory in the 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, each process will store this data in a self-loop data, and the numbert of this process will be 120000.

The result I'm testing:

do not store this data, memory will be:

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

      

save this data and momory will be:

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

      

So the big difference is that memoery is used proc

:(1922806 - 1684032) / 1024 = 233M.

After researching, I will find a 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

      

Only 6 words are used in memory after using binary? How can this be explained?

+3


source to share


2 answers


There are two useful functions for measuring the size of an Erlang member: erts_debug:size/1

and erts_debug:flat_size/1

. Both of these functions return the word size in words.

erts_debug:flat_size/1

gives the total message size without optimizing timing. It is guaranteed that the size of this word will be copied to the new heap, just like with message transfers and ets tables.

erts_debug:size/1

gives you the size of the term as it is on the heap of the current process, allowing you to optimize memory usage by sharing duplicate terms.

Here's a demo 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 size due to time divisions.

As for your specific term, I used the Erlang wrapper (R16B03) and measured the term flat_size

. According to this the memory usage of your term: 226 words (1808B, 1.77KB).

That's a lot of memory to use for what seems like a simple term, but that's beyond the scope of this question.

+6


source


the size of the whole binary is 135 bytes, if you execute 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 lost direct access to the internal structure.

Strange, but you can understand:



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 erts_debug: flat_size returns the size of the variable (this is roughly the type information, pointer to data and its size), but not the size of the binary data itself. Binary data is stored in a different location and can be shared by different variables.

+3


source







All Articles