All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is a Garbage Collector in Ruby? How does it work and what is GC needed for?

This content has been automatically translated from Ukrainian.
Garbage Collector in Ruby is an automated memory management mechanism responsible for monitoring and freeing memory that is no longer used by the program. The GC ensures that memory allocated for objects that are no longer needed by the program can be reused, thereby optimizing system resources.
Just imagine that there are objects in memory that are unnecessary. Their number keeps increasing. A memory leak occurs. The server may stop responding, and in the case of scaling, your costs will only increase. So, GC is needed for efficient use of RAM.
The Garbage Collector in Ruby works on the principle of "mark-and-sweep". The garbage collector traverses all objects that can be reached from root objects (such as local variables and global variables) and marks (mark) them as "reachable". After the marking process is complete, the collector moves to the sweeping phase (sweep), where it frees memory from all objects that were not marked. These objects are considered "unreachable" and their memory (the memory they previously occupied) can be reused.
Ruby also uses the concept of generations (Generational Garbage Collection or GGC), which involves dividing objects into generations based on their usage frequency, allowing for more efficient memory management by focusing the garbage collector's efforts on younger, more dynamic objects that change more frequently.
GC is usually discussed when a Memory Leak occurs on the server. Of course, restarting the server will help for a while (RAM will be cleared and start filling up again), but this issue will still need to be addressed.
Like it?React
🧵

This post doesn't have any additions from the author yet.

ZOMBIE in Ruby. What is it?
May 3, '24 12:41

ZOMBIE in Ruby. What is it?

Does Ruby create a new copy of an object when assigning a variable to another variable?
May 29, '24 09:30

Does Ruby create a new copy of an object when assigning a variable to another variable?

What is jemalloc and how does it relate to Ruby / Ruby on Rails
May 30, '25 11:53

What is jemalloc and how does it relate to Ruby / Ruby on Rails

What is the difference between immediate value and reference in Ruby?
May 29, '24 12:00

What is the difference between immediate value and reference in Ruby?

Apr 14, '24 09:14

Accept - HTTP header, what is it for and how does it work?

A bit about Ruby implementation types (CRuby (MRI), JRuby, Rubinius, TruffleRuby, mruby)
May 5, '24 12:36

A bit about Ruby implementation types (CRuby (MRI), JRuby, Rubinius, TruffleRuby, mruby)

What is a prompt and prompt engineering?
May 3, '24 12:08

What is a prompt and prompt engineering?

May 7, '24 07:24

What is native machine code?

Enabling YJIT in Ruby 3.2.1 (Ruby on Rails)
May 8, '24 07:57

Enabling YJIT in Ruby 3.2.1 (Ruby on Rails)

May 9, '24 12:43

[Fix] Rails Admin - undefined local variable or method javascript_importmap_shim_nonce_configuration_tag

What is technical debt in IT projects?
May 13, '24 06:17

What is technical debt in IT projects?

Apr 27, '24 09:29

What are CC and BCC in emails? What are they for and how to use them?

May 13, '24 07:11

What does scope mean in IT project management?

Apr 18, '24 06:06

Analyzing the PG::ObjectInUse error (Ruby on Rails)

Apr 18, '24 05:27

We fix ActiveRecord::ProtectedEnvironmentError in the local environment.

Apr 15, '24 18:11

What are HTTP Client Hints?