variables passed as parameters

Richard Carlsson <>
Fri May 12 23:00:24 CEST 2006

Danie Schutte wrote:
> when a list / gb_tree is passed as a parameter, is a full memcopy of 
> the data done to the new function?

No, if it's just a function call, then all data structures are passed
by reference - there is no copying. Atoms, "small" integers (<2^27),
etc., that fit into a single word anyway are always copied, but it
does not matter, because they cannot be manipulated in memory.

> The reason for my question:  We would like to a have a data set 
> passed on through various modules, and each module may / may not 
> update this data..

As long as it's the same _process_ that is passing on the data from
function to function (perhaps in different modules), there is no
copying. If one function "updates" the data (a list or tree), it
actually just creates new references into the old data, and often
discards some of the old references. (Any parts of the data that can
no longer be reached through a reference, are automatically recycled
by the garbage collector.)

However, when you pass a data structure to another process, it
will be copied, because processes do not share "heap memory" (in
the standard "private heap" Erlang runtime system). And if processes
are on different nodes, there will of course always be copying.

Side note: If you use the (still considered experimental?) "hybrid heap"
version of the runtime system, or the (now discontinued, also
experimental) "shared heap" version, the amount of copying between
processes on the same node is reduced.

> We are trying to figure out if it would be faster to create a single 
> module that manages the data in a "memory space" (like a gen_server 
> state variable or an ets table) or wether it would not really matter 
> in terms of speed - and the only factor to watch out for would be 
> memory?

It's important to understand that _modules_ are not responsible for
memory spaces. _Processes_ are. So, a gen_server process has its own
state and memory space, and likewise an ets table is actually behaving
as if it was just another process (although it is in fact implemented
in C for the sake of high efficiency). When you enter data in an ets
table, or look up data, the data is always copied from your process
to the table, or from the table to the process, as if by sending


More information about the erlang-questions mailing list