[erlang-questions] simple question about list memory consumption

Richard O'Keefe <>
Tue Jul 10 03:21:42 CEST 2012


On 9/07/2012, at 10:34 PM, CGS wrote:
> > About the two lists you were speaking about (I suppose you were referring to the output of lists:map/2 and lists:seq/2), the second is destroyed as soon as lists:map/2 exits.
> 
> No it isn't.  It is only reclaimed when the garbage collector runs.
> 
> That is a bit puzzling.

It seems that you are muddling up "is LIVE" with "EXISTS".
When you compute
	map(F, L)
and there are no other references to L anywhere,
L becomes *DEAD* as soon as the call to map/2 completes.
(In fact, each cell of L becomes dead in turn during the
call.)  But DEAD and DESTROYED are very different properties.
L is not "destroyed" until the garbage collector runs;
until the garbage collector runs the cells of L still occupy
space on the heap.  When, if ever, that space is reclaimed
depends on how much other need there is for it.

> Looking at the code for lists:map/2:
> 
> map(F, [H|T]) ->
>     [F(H)|map(F, T)];
> map(F, []) when is_function(F, 1) -> [].
> 
> I can see that when lists:map/2 exits, the only remaining list is an empty list.

No you can't see that, because there is nothing in the code
for map/2 to release any storage whatever.

> I might be wrong, but I see only one word remaining for the garbage collector to take care of.

What do you think makes the other words go away?
> 
> 
> You got the result from process_info, I got it from `free' under Linux.

Not the best way to do it.  That tells you how much memory
the operating has handed out but not to which process; even
ps -o size tells you only how much memory Erlang has claimed from
the operating system, and nothing about what Erlang is doing with
that memory.





More information about the erlang-questions mailing list