Storage management in Elixir

how is memory management handled in Elixir?
dynamic allocation or static block of memory?

1 Like

It totally depends on your implementation of the BEAM-VM.

Ericssons implementation (which is considered normative)[1] does request memory from the OS as necessary, and also gives it back occasionally :wink: So from the OS perspective it is dynamic.

[1] : To be honest, I am not aware of any other implementation :wink:

2 Likes

From the application-programmer point of view, this is handled ‘for you’. The only things you need to know is that the BEAM virtual machine will do its best to only copy data when you really need to.

Looking under the hood:
The Actor-Model paradigm means that every actor (Elixir/Erlang process) has its own bit of memory. This memory is allocated by the BEAM when the process is created (I believe by default at a size of 4 kilobytes). One side of this memory is considered the stack, the other side the heap. Inside this blob of memory, a certain data structure (usually) only exists once; underwater, references (/pointers?) to this structure are manipulated when you bind a (new) name to (part of) such a structure.

Data is copied only when one process sends a message to another.

This memory that starts out at 4 kb is doubled when it becomes full. The nice thing about this memory per-process, is that garbage collection can also be done per-process (because data is not shared between processes), meaning that all other cores on your computer can continue executing all other processes in parallel.

As @NobbZ already pointed out: Above explanation is true for Ericsson’s implementation, but other BEAM implementations might do the memory management differently.

So: The nominative BEAM implementation uses dynamic allocation by requesting from the OS double the size of a process’ memory space when it is full, and from time to time (such as after garbage collection), memory is given back to the OS.

2 Likes

Thank you I appreciate it

1 Like

As to other implementations of the BEAM, there’s erjang that implements BEAM on top of JVM.

2 Likes

More detailed information directly from the horse’s mouth:

https://www.erlang-solutions.com/blog/erlang-19-0-garbage-collector.html

If you google Lukas Larsson and erlang you will find links to a number of interesting talks and blogs about BEAM internals.

2 Likes

There was a Java Erlang implementation a long long time ago that never got fully up since the JVM made certain acts impossible, so it was fairly restrictive. It made math fast though due to tracing JIT.

Ooo, speaking of there it is! It still ‘exists’ it seems though not really developed either. ^.^

Erjang still exists and is actually a very good implementation of the BEAM. It can do almost everything for running erlang, for example I have run it an the BEAM together in a distributed system running mnesia. Wouldn’t be surprised if it could handle elixir as well. Though I don’t know if it supports 19 yet.

1 Like