[erlang-questions] memory allocation and constant pool behavior

Raimo Niskanen <>
Mon May 23 16:39:54 CEST 2011

On Mon, May 23, 2011 at 09:02:48AM -0500, James Hague wrote:
> The only memory allocation done at runtime in your function is for a
> single two-element tuple containing pointers to existing tuples. It's
> the per tuple overhead + 2 words. According to the efficiency guide,
> tuple overhead is 2 words, giving a total of 4.
> (I had previously thought that the tuple overhead was 1 word, but the
> efficiency guide claims it to be 2.  Did that change at some point or
> have I always been mistaken?)

Yes. Yes. No. (In that order)

If you compare with e.g String you see it is annotated as
1 word + 2 words per element, and as we know it is one
2-word cons cell per character. The first mentioned
1 word is the handle to the list itself, either on the stack
or on the heap in some other element.

So the heap overhead for a tuple is 1 word, but there is a handle
to the tuple somewhere too.

For example: f({{1,2,3}}) causes one word on the stack
that points to the outer 1-tuple on the heap where it
occupies 2 words; the arity and one handle for the inner
3-tuple that occupies 4 heap words - total 7.
You can view this as 2+3 caused by the inner tuple
(Overhead+Payload) and 2+InnerTuple in the outer
tuple (also Overhead+Payload).

> _______________________________________________
> erlang-questions mailing list
> http://erlang.org/mailman/listinfo/erlang-questions


/ Raimo Niskanen, Erlang/OTP, Ericsson AB

More information about the erlang-questions mailing list