Skip to content
On this page

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

module · Ruby 4.1

GC

1 instance methods 23 class methods

The GC module provides an interface to Ruby’s mark-and-sweep garbage collection mechanism.

Some of the underlying methods are also available via the ObjectSpace module.

You may obtain information about the operation of the GC through GC::Profiler.

Class methods 23

Instance methods 1

Constants

Internal constants in the garbage collector.

GC build options

Ruby version changes 10

  1. 4.1
    • GC.stat accepts scope: :ractor (the default), scope: :local (a synonym for :ractor), and scope: :global. With the default collector, the global scope reports cumulative collection counts and measured marking/sweeping CPU time across Ractors, including history from destroyed object spaces, without requesting a stop-the-world snapshot. Unscoped GC.stat, GC.count, and GC.total_time retain their current-Ractor behavior.

    • GC.stat reports a new key, :global_gc_count: the number of stop-the-world global garbage collection cycles the current Ractor initiated. Each global collection is attributed to exactly one Ractor, so the values sum across Ractors. GC.stat(:global_gc_count, scope: :global) reports the process total, including the counts of terminated Ractors.

    • A global garbage collection is not counted in :major_gc_count. :count equals :minor_gc_count plus :major_gc_count plus :global_gc_count in both scopes. When another Ractor’s object space exists, including one left by a terminated Ractor that is not yet absorbed, or one was absorbed since the last global collection, GC.start runs a global collection by default, so it increments :global_gc_count rather than :major_gc_count; pass global: false for a local major collection.

    • Global compaction no longer counts overlapping CPU intervals in scalar GC timing statistics. Profiler wall-time intervals are unchanged.

  2. 3.4
    • GC.config added to allow setting configuration variables on the Garbage Collector. [Feature #20443]

    • GC configuration parameter rgengc_allow_full_mark introduced. When false GC will only mark young objects. Default is true. [Feature #20443]

  3. 3.2
  4. 3.1
  5. 3.0
    • GC.auto_compact= and GC.auto_compact have been added to control when compaction runs. Setting auto_compact= to true will cause compaction to occur during major collections. At the moment, compaction adds significant overhead to major collections, so please test first! [Feature #17176]