[erlang-questions] simple question about list memory consumption
Fri Jul 6 14:29:12 CEST 2012
An Erlang list uses at least 2 words per element - one word for the
element itself, and one word to point to the next list element. (Of
course, if the list element doesn't fit in a word, it would need
space for that too, but ASCII characters do fit.)
So on a 32-bit machine, you'd expect an ASCII string to use 8 (2 * 4)
bytes per character, and on a 64-bit machine it should be 16 (2 * 8)
bytes per character (unless you're using the halfword emulator).
I'm not sure why you see 21 bytes per character, though...
If you need more memory-efficient storage, consider using binaries
instead. They use one byte per, well, byte; however, some
completely different considerations apply:
Hope this helps,
----- Original Message -----
> Hi everyone,
> I am puzzled by a behavior I didn't expect, so, if anyone can help to
> understand it, I would appreciate.
> Let's consider a string of latin1 characters. When dumped to a file
> on a harddisk it occupies, as expected, 1 B per character. When
> loaded in a variable in Erlang shell, I get about 21 B per character
> (at least free is reporting that). Of course, it is hard to test on
> one character due to changing RAM consumption of other processes.
> So, my test case consisted in (I voided writing down and reading
> from a file in the test case):
> 1. stabilizing the environment (no other processes were started, but
> the OS ones and watching for the RAM consumption stabilization at
> the level of MB - give or take 1MB);
> 2. opening an Erlang shell (few MB consumption - stable);
> 3. executing:
> L = lists:map(fun(_) -> 107 end,lists:seq(1,10000000)), ok.
> where (I know it's obvious, but just for completeness):
> - 107 is representing "k" character;
> - the 'ok' atom at the end is for not writing the content of L in the
> - the list [107,107,...] is equivalent to "kk..." string (which
> written in a plain text file it gives 1 B per character as it is
> The occupied RAM jumped by 214-217 MB for 10^7 characters (at 10^6
> characters, I got 21 MB, so, it is related to the string length
> only), which gives about 21 B per character (one can compute within
> the errors the exact the size, but I took that value as the minimum
> Erlang shell reports "Erlang R15B01 (erts-5.9.1) [source] [smp:4:4]
> [async-threads:0] [hipe] [kernel-poll:false] [dtrace]".
> I know that this isn't a big problem, but I am missing something here
> for sure. Can anyone point what I am missing? Thank you in advance.
> erlang-questions mailing list
Erlang Solutions Ltd
More information about the erlang-questions