Reputation: 11251
I am new to Erlang and highly interested in a hybrid memory model
. I have several questions about it.
message
? Do Erlang pass serialized objects or objects by themselves to shared memory heap? garbage collector
knows which message is never used any more and can be deleted? Upvotes: 3
Views: 88
Reputation: 8340
I recommend that you read Joe Armstrong's Phd where he explains in great details reasons behind various design decisions that he had to make when he was writing the first implementation of Erlang.
Message passing is explained on page 25:
2.4.5 Message passing
Message passing obeys the following rules:
- Message passing is assumed to be atomic which means that a message is either delivered in its entirety or not at all.
- Message passing between a pair of processes is assumed to be ordered meaning that if a sequence of messages is sent and received between any pair of processes then the messages will be received in the same order they were sent.
- Messages should not contain pointers to data structures contained within processes—they should only contain constants and/or Pids.
To answer what hasn't been answered by the above:
Erlang is a functional language, it has nothing to do with objects. The only compound data structures you can pass between processes are tuples, maps and lists. There is no shared state in Erlang so the message is copied between processes. The copy may be actually implemented by passing a pointer to a shared memory fragment by BEAM (Erlang virtual machine) but it's an optimization that's hidden from Erlang itself. And there are no pointers in Erlang either so it's not possible to access one process' memory from another process' code (even intentionally).
All data structures are the same with respect to the garbage collector - if data is not referenced by the process then it can be deleted from the memory. When the message arrives it's held in the process inbox, so it's referenced. When it's taken from inbox it's processed by the process code in the same way as any other variable - if it's not used any more then it's deleted.
Upvotes: 5