why the memory for atom is zero using erts_debug:s

2019-07-21 13:28发布

问题:

I use erts_debug:size/1 to calculate the memory for atom in erlang VM, but i find that the output is zero. Who can explain the reason?

7> erts_debug:size(true).
0

回答1:

The reason is that atoms are interned in the atom table together with the data for the atom so there is only one copy of an atom in the whole node. This means that in your data the atom is just a tagged reference in to the atom table and takes no space. Hence the size is zero.

So it is not an inconsistency or bug.



回答2:

In docs, you can read:

%% size(Term)
%%  Returns the size of Term in actual heap words. Shared subterms are
%%  counted once.  Example: If A = [a,b], B =[A,A] then size(B) returns 8,
%%  while flat_size(B) returns 12.

The keyword here is HEAP. Process has stack and heap. There is a great presentation, that shows you, what gets stored on the heap and what on the stack, when a term is created (start reading from page 8).

http://www.erlang-factory.com/upload/presentations/467/Halfword_EUC_2011.pdf

Basically, when you create a single atom. There is nothing on the heap and a one word pointer on the stack. It points to the atom table, which also consumes memory and is not garbage collected (never create atoms dynamically in your application!). Source: http://www.erlang.org/doc/efficiency_guide/processes.html



标签: erlang