[erlang-bugs] Loss of sharing

Bjorn Gustavsson bjorn@REDACTED
Wed Nov 7 06:35:01 CET 2007


"John Hughes" <john.hughes@REDACTED> writes:

> Try running hog:main() in the program below. It creates a data structure in
> one process, measuring heap size, and how it changes during the creation.
> Then it sends it to another, measuring the memory allocated in the receiving
> process. Here's the output:
> 
> Creating tree:: allocated 0 bytes, heap size 987 bytes
> 
> Receiving tree:: allocated 116769407 bytes, heap size 116769640 bytes
> 
> true
> 
> That is, the structure is less than 1K bytes in the sender, but over 100MB
> in the receiver.
> 

For the traditional type of applications that Erlang is typically is used
for, loss of sharing is not an issue. There is typically no sharing at all.
Therefore term copying between process is optimized for speed.

> 
> The reason is that it contains  a lot of sharing, and sharing is lost when
> messages are sent to other processes. Sharing is ALSO lost when values are
> copied to a new process being spawned. In fact, I've found no way to copy
> structures between processes WITHOUT losing sharing. This means that some
> data simply cannot practically be sent from one process to another.

Correct.

> 
> Any chance of introducing a sharing-preserving copy in message send and
> spawn? It would be really useful!

It is not likely that it will become the default behaviour. But it could be
possible to make it possible as an option, for instance a new option for the
erlang:send/3 BIF.

/Bjorn

-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB



More information about the erlang-bugs mailing list