Ruby/Rails Garbage Collection

Garbage collector in Ruby on Rails?

Rails is a framework, not a language. The language behind Rails is called Ruby.

This means there is no notion of Garbage Collector in Rails. You should search for documentation about the Ruby Garbage Collector.

You can start from the Ruby GC module. The GC module provides an interface to Ruby’s mark and sweep garbage collection mechanism.

Depending on the Ruby language version, the Garbage Collector may have a different behavior. The article How Ruby Manages Memory and Garbage Collection describes the Ruby 1.9 Garbage Collector. In Ruby 2.0 the GC has been improved and the implementation changed a bit.

Ruby / Rails garbage collection

Simple answer: the Ruby runtime has a garbage collector. Depending on the runtime (JRuby/JVM generational GC, IronRuby/CLR generational GC, classic Ruby/mark-sweep GC) different algorithms are used. But the basics are pretty simple:

  • Upon an allocation request if there is "insufficient free memory" available - how much is insufficient is one of the ingredients of the GC algorithm - then a GC will commence
  • The GC starts by scanning roots, which are global variables and stack locations (parameters and local variables), to discover which objects are still alive; it marks each object it finds
  • Then, the GC process looks at links (references) inside these objects, and recurses into those objects that haven't already been marked
  • The GC can then start moving / copying all marked objects so that they are compacted in memory
  • The "free pointer", from whence new allocations occur, is reset to the end of this compacted chunk of memory
  • If there is still "insufficient free memory", then more is allocated from the operating system
  • All old objects that weren't marked during the scanning process are garbage and are implicitly discarded through the copying process and resetting of the free pointer.

The frequency of collections depends on the tuning of the GC, which may be affected by the operating system, the amount of physical memory, operating system memory pressure, user-controlled tweaks, underlying platform version revisions, dynamically optimized parameters, etc. Much of it comes down to deciding where the bar lies in that "insufficient free memory" test, though things get more complicated with generational collectors.

Garbage collect in ruby?

No, there is no way to do it in Ruby.

There is a method called GC::start, and the documentation even says:

Initiates garbage collection, even if manually disabled.

But that is not true. GC::start is simply an advisory from your code to the runtime that it would be safe for your application to run a garbage collection now. But that is only a suggestion. The runtime is free to ignore this suggestion.

The majority of programming languages with automatic memory management do not give the programmer control over the garbage collector.

If Ruby had a method to force a garbage collection, then it would be impossible to implement Ruby on the JVM and neither JRuby nor TruffleRuby could exist, it would be impossible to implement Ruby on .NET and IronRuby couldn't exist, it would be impossible to implement Ruby on ECMAScript and Opal couldn't exist, it would be impossible to implement Ruby using existing high-performance garbage collectors and RubyOMR couldn't exist.

Since it is in generally desirable to give implementors freedom to implement optimizations and make the language faster, languages are very cautious on specifying features that so drastically restrict what an implementor can do.

I am quite surprised that R has such a feature, especially since that means it is impossible to implement high-performance implementations like FastR in a way that is compliant with the language specification. FastR is up to more than 35× faster than GNU R, so it is obvious why it is desirable for something like FastR to exist. But one of the ways FastR is faster, is that it uses a third-party high-performance garbage collected runtime (either the GraalVM or the JVM) that does not allow control over garbage collection, and thus FastR can never be a compliant R implementation.

Interestingly, the documentation of gc() has this to say:

[T]he primary purpose of calling gc is for the report on memory usage.

Why isn't this simple class garbage collected in Ruby console?

It seems this was already answered in another question

Pry stores the output of the last 100 commands and thus the object can't actually be garbage collected.



Related Topics



Leave a reply



Submit