[erlang-questions] learner's questions -- tuples, CEAN & jungerl

James Hague james.hague@REDACTED
Mon Apr 25 16:00:46 CEST 2011


> 1. Found the following usage of tuple --
>        { person, { name, joe }, { age, 42 } }
>     Now when one has millions on such tuples, which are held in-memory,
> isn't the memory footprint of the application at-least 3  x 8-byte
> extranuous thanks to the 3 atoms (person, name, age), playing "field names"

A tuple uses 1 + tuple_size cells, where a cell is either 32 or 64
bits, In your example:

   {age, 42} = 3 cells
   {name, joe} = 3 cells
   {person, _, _} = 4 cells

the total is 10 cells, or 40 bytes (double that if you're running the
full 64-bit version of the Erlang).

Personally, I don't think the memory usage is any kind of issue here.
I like to use lists of {Key, Value} tuples, which has even more
overhead. But if you've got some special case where you need millions
of such structures, then you could trade away flexibility in exchange
for lower memory usage and just go with {Age, Name}.  That's only 3
cells instead of 10. (And if really pressed, there's [Age|Name] which
drops the count to 2 cells.)



More information about the erlang-questions mailing list