Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why the memory for atom is zero using erts_debug:size/1?

Tags:

erlang

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
like image 506
BlackMamba Avatar asked Aug 28 '14 07:08

BlackMamba


2 Answers

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.

like image 174
rvirding Avatar answered Sep 20 '22 19:09

rvirding


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

like image 26
tkowal Avatar answered Sep 20 '22 19:09

tkowal