Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
GC.start
# File tmp/rubies/ruby-4.1.0-preview1/gc.rb, line 64
def self.start full_mark: true, immediate_mark: true, immediate_sweep: true, global: true
Primitive.gc_start_internal full_mark, immediate_mark, immediate_sweep, false, global
end
Initiates garbage collection, even if explicitly disabled by GC.disable.
Keyword arguments:
-
full_mark: a boolean value specifying whether to perform a major garbage collection cycle:-
true: initiates a major garbage collection cycle, meaning all objects (old and new) are marked. When another Ractor’s object space exists (a liveRactor, or a terminated one not yet absorbed), or one was absorbed since the last global collection, andglobalistrue(the default), the cycle is a global collection instead; seeglobal. -
false: initiates a minor garbage collection cycle, meaning only young objects are marked.
-
-
immediate_mark: a boolean value specifying whether marking is performed incrementally:-
true: marking is completed before the method returns. -
false: marking is performed in parts, interleaved with program execution both before the method returns and afterward; therefore marking may not be completed before the return. Note that iffull_markisfalse, marking is always immediate, regardless of the value ofimmediate_mark.
-
-
immediate_sweep: a boolean value specifying whether sweeping is deferred (lazy sweep):-
true: sweeping is completed before the method returns. -
false: sweeping is performed in parts, interleaved with program execution both before the method returns and afterward; therefore sweeping may not be completed before the return.
-
-
global: a boolean value specifying, when another Ractor’s object space exists, whether to collect every Ractor’s heap together in a single stop-the-world mark/sweep cycle:-
true: with another Ractor’s object space present (seefull_mark) it runs a stop-the-world cycle and reclaims shareable and cross-Ractor garbage. This option, although it defaults totrue, is ignored unlessfull_mark,immediate_markandimmediate_sweepare alltrue. With a single object space it has no effect and the other options apply as usual. This option is also ignored unless you’re using the defaultGC. -
false: collect only the calling Ractor’s own objects. Shareable objects and cross-ractor garbage are not swept.
GC.statcounts a local major collection under:major_gc_count, a local minor collection under:minor_gc_count, and a global collection under:global_gc_countfor the callingRactor. -
Note that these keyword arguments are implementation- and version-dependent, are not guaranteed to be future-compatible, and may be ignored in some implementations.
Ruby version changes
-
4.1
-
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.
-