[erlang-questions] simple question about list memory consumption

CGS cgsmcmlxxv@REDACTED
Mon Jul 9 12:34:17 CEST 2012


On Mon, Jul 9, 2012 at 1:39 AM, Richard O'Keefe <ok@REDACTED> wrote:

>
> On 9/07/2012, at 7:41 AM, CGS wrote:
>
> > Hi,
> >
> > 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. 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. I might be wrong, but I see only one word remaining for the garbage
collector to take care of. That means, at the end, there is only 1 full
list remaining (1 word really can be neglected compared with 214 MB which I
got for 10^7 characters long list).



>
> > On a 64-bits machine, 2 words = 16 B and 3 words = 24 B, so, my value of
> 21 B is exactly in between 2 and 3 words. I suppose you worked on 32-bits
> machine or with half-world emulator and you got half of that value (or a
> bit less if we are to consider that the shell is adding bigger overhead).
>
> No, I used process_info, which reports the size in words, not bytes.
>

You got the result from process_info, I got it from `free' under Linux. In
the end, I don't suppose it matters too much how you or I got the info, but
it matters that the results are compatible and, moreover, they are
compatible with the official documentation, don't you think? The results
are consistent with 2 words per character in the list. The remaining is an
artifact from shell and/or memory management (which can come from lower
levels as well). My main question here was because I forgot about the list
memory management in Erlang (as described in the efficiency guide) and I
was wondering about 1 B vs. 21 B per character. As I said, I knew I was
missing something and I was looking for someone to help me remember. :)
Thank you all for investing time in answering my question.

CGS
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120709/bcb590ec/attachment.htm>


More information about the erlang-questions mailing list