Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.


Ruby uses automatic memory management. The garbage collection runs when the process's allocated memory runs out. Ruby's garbage collection uses "mark and sweep", which goes through every object it can access through others, and those that are not accessible and therefore not marked, after which they are "sweeped" (garbage collected). Although this method is simple and efficient, it has performance drawbacks because looking at every object can be time consuming and reduce performance. To combat this, Ruby 2 .1 introduced generational garbage collection, which starts objects out on the "young" generation and moves them to the "old" if they survive many sweeps. When there is no room among the young, an object is put into the "old" space. This system means less time spent looking over every object, instead focusing on ones more likely to need removal.Printing GC

Here are some useful lines of code that help with manipulating memory:

C.stat(:total_freed_objects) shows how many times the garbage collector was called.

The following code will gradually free memory:

while true 

Including this code will disable garbage collection in your program.

GC.disable   #=> true