Garbage collection and memory management in Erlang

user1002288 picture user1002288 · Apr 19, 2012 · Viewed 9.2k times · Source

I want to know technical details about garbage collection (GC) and memory management in Erlang/OTP.

But, I cannot find on erlang.org and its documents.

Some articles online are only general talk without touching details, such as what garbage collection algorithm is used, what is performance overhead, memory allocation is also like heap in C?

Answer

Hamidreza Soleimani picture Hamidreza Soleimani · Jan 9, 2016

To classify things, lets define the memory layout and then talk about how GC works.

Memory Layout

In Erlang, each thread of execution is called a process. Each process has its own memory and that memory layout consists of three parts: Process Control Block, Stack and Heap.

enter image description here

  • PCB: Process Control Block holds information like process identifier (PID), current status (running, waiting), its registered name, and other such info.

  • Stack: It is a downward growing memory area which holds incoming and outgoing parameters, return addresses, local variables and temporary spaces for evaluating expressions.

  • Heap: It is an upward growing memory area which holds process mailbox messages and compound terms. Binary terms which are larger than 64 bytes are NOT stored in process private heap. They are stored in a large Shared Heap which is accessible by all processes.


Garbage Collection

Currently Erlang uses a Generational garbage collection that runs inside each Erlang process private heap independently, and also a Reference Counting garbage collection occurs for global shared heap.

  • Private Heap GC: It is generational, so divides the heap into two segments: young and old generations. Also there are two strategies for collecting; Generational (Minor) and Fullsweep (Major). The generational GC just collects the young heap, but fullsweep collect both young and old heap.

  • Shared Heap GC: It is reference counting. Each object in shared heap (Refc) has a counter of references to it held by other objects (ProcBin) which are stored inside private heap of Erlang processes. If an object's reference counter reaches zero, the object has become inaccessible and will be destroyed.


To get more details and performance hints, just look at my article which is the source of the answer: Erlang Garbage Collection Details and Why It Matters