On this page
Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
GC
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
Internal constants in the garbage collector.
GC build options
Ruby version changes 10
-
4.1
-
GC.statacceptsscope: :ractor(the default),scope: :local(a synonym for:ractor), andscope: :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. UnscopedGC.stat,GC.count, andGC.total_timeretain their current-Ractor behavior. -
GC.statreports a new key,:global_gc_count: the number of stop-the-world global garbage collection cycles the currentRactorinitiated. Each global collection is attributed to exactly oneRactor, 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.:countequals:minor_gc_countplus:major_gc_countplus:global_gc_countin both scopes. When another Ractor’s object space exists, including one left by a terminatedRactorthat is not yet absorbed, or one was absorbed since the last global collection,GC.startruns a global collection by default, so it increments:global_gc_countrather than:major_gc_count; passglobal: falsefor a local major collection. -
Global compaction no longer counts overlapping CPU intervals in scalar
GCtiming statistics. Profiler wall-time intervals are unchanged.
-
-
3.4
-
GC.configadded to allow setting configuration variables on the Garbage Collector. [Feature #20443] -
GCconfiguration parameterrgengc_allow_full_markintroduced. WhenfalseGCwill only mark young objects. Default istrue. [Feature #20443]
-
-
3.2
-
Expose
need_major_gcviaGC.latest_gc_info. GH-6791
-
-
3.1
-
“GC.measure_total_time = true” enables the measurement of
GC. Measurement can introduce overhead. It is enabled by default.GC.measure_total_timereturns the current setting.GC.stat[:time] orGC.stat(:time) returns measured time in milli-seconds. [[Feature #10917]] -
GC.total_timereturns measured time in nano-seconds. [[Feature #10917]]
-
-
3.0
-
GC.auto_compact=andGC.auto_compacthave been added to control when compaction runs. Settingauto_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]
-