About Garbage Collection
Ş. Volkan Erdoğan
volerdo@REDACTED
Wed Nov 10 07:19:54 CET 2010
Hi,
I have an idea about memory management in Erlang and although the idea
is still raw I'd like to share with you.
As far as I know:
- There are no global variables in Erlang. Memory is allocated in functions.
- Function calls in processes and message passing operations to other
processes have copying semantics.
So I believe that these properties guarantee that any memory location
allocated by runtime is exclusive to the function which requested that
memory and it is not possible for a reference of a memory location to
escape from the function that created it. And as a result it is
possible to safely deallocate all memory allocated by the function at
function exit since those memory locations can only be accessed by
that function. However I think it is possible to do better because
Erlang has single assignment property and one can easily find the last
location that a variable is used and can safely deallocate memory
after that location. Also we can relax copying semantics for some type
of function calls and pass a reference to the functions since compiler
prevents variable modification (I am not sure if runtime is already
doing this). To enable these operations following modifications are
needed to be done in Erlang runtime and compiler:
- Erlang virtual machine will need a new instruction for memory
deallocation operations (This operation will be called DEL)
- The compiler should pass references for parameters instead of
copying them during function calls
After these modifications are done, the analysis algorithm described
below can be executed for each function before that function is loaded
to the virtual machine
For each variable including the formal parameters of the function
-Find last usage of the variable
-If last usage of the parameter is in a function call
-do nothing (since it will be analyzed or already analyzed in that
function as formal parameter)
-Else
-Insert a DEL instruction for that variable just after the usage
To sum up I believe the properties of the language lets an analyzer to
exactly find the life times of variables created in functions (btw ETS
tables might need special care). This is much like working with heap
memory in C, only this time an external program is inserting free
operations.
More information about the erlang-questions
mailing list