Blog: Demystifying Ruby 3/3 : Memory Management
Garbage collection (GC) is an essential process in any programming language that manages memory for the programmer. In Ruby (this article is about the MRI), the garbage collector plays a crucial role in ensuring efficient memory usage and preventing memory leaks.
The Heap and the Stack
Memory in a Ruby program is divided into two main regions:
- The Heap: This is where dynamically allocated memory lives. Objects created at runtime, such as strings, arrays, and hashes, are stored here. The garbage collector primarily operates on the heap to free up unused memory.
- The Stack: This is where method calls, local variables, and control flow data are stored. The stack is managed automatically and follows a Last In, First Out (LIFO) structure. Each time a method is called, a new stack frame is created; when the method completes, its stack frame is removed.
Ruby’s garbage collector is responsible for:
[Read More]