[erlang-questions] simple question about list memory consumption

ok <>
Sun Jul 8 07:22:36 CEST 2012

> Hm, ignore my previous answer, then; I was under the impression that
> small enough integers would be stored as immediates in the
> list cell...

They are.
See the Efficiency guide.
"String ... 2 words per character"

Each list cell holds one integer, which *is* an immediate in the
list cell and points nowhere else, and one pointer to the next
element in the list.

>> 2. opening an Erlang shell (few MB consumption - stable);
>> 3. executing:
>> L = lists:map(fun(_) -> 107 end,lists:seq(1,10000000)), ok.

Two things strike me about this.
(A) you construct TWO lists here, so at 8 bytes per list cell
on a 32-bit machine, there's 16 bytes each already.
(B) You are doing this in the shell, where expressions are
interpreted.  I have no idea what space overheads that adds.

Is there a way of measuring the amount of heap space currently
_used_ in a process?  process_info>heap_size and
process_info>total_heap_size seem to be about the amount of
space allocated for a process, including its stack and any quantity
of free space.  So the following only gave me an approximate idea:


heap_size() ->
    element(2, process_info(self(), heap_size)).

ss(N) ->
    Before = heap_size(),
    Result = loop(N),
    After  = heap_size(),
    (After-Before)*1.0 / length(Result).

loop(0) ->
loop(N) ->
    [$k | loop(N-1)].

However, the idea it _did_ give me was "between 2 and 3 words
per character", so it looks like "2 words per character" as
stated in the Efficiency Guide is, after all, true.

It's certainly nowhere near 21 bytes.

More information about the erlang-questions mailing list