Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Garbage collection and memory management in Erlang

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.

I have found some articles online which talk about GC in a very general manner, such as what garbage collection algorithm is used.

like image 406
user1002288 Avatar asked Apr 19 '12 05:04

user1002288


People also ask

Does Erlang have 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.

What is garbage collection in loadrunner?

A Garbage Collector is a Java program which tracked the referenced (live) objects and allowed them to keep in the heap memory whereas the memory of the unreferenced (dead) objects is reclaimed and reused for future object allocation. This method of reclaiming the unused memory is known as Garbage Collection.

Which memory is cleared in garbage collection?

Garbage collection makes Java memory efficient because it removes the unreferenced objects from heap memory and makes free space for new objects. The Java Virtual Machine has eight types of garbage collectors.

What is process in Erlang?

Erlang processes are lightweight, operate in (memory) isolation from other processes, and are scheduled by Erlang's Virtual Machine (VM). The creation time of process is very low, the memory footprint of a just spawned process is very small, and a single Erlang VM can have millions of processes running.


1 Answers

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

like image 175
Hamidreza Soleimani Avatar answered Oct 03 '22 21:10

Hamidreza Soleimani