Skip to content

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

class method

GC.start

(?immediate_sweep: ::boolish, ?immediate_mark: ::boolish, ?full_mark: ::boolish) -> nil
start(full_mark: true, immediate_mark: true, immediate_sweep: true, global: true)

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 live Ractor, or a terminated one not yet absorbed), or one was absorbed since the last global collection, and global is true (the default), the cycle is a global collection instead; see global.

    • 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 if full_mark is false, marking is always immediate, regardless of the value of immediate_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 (see full_mark) it runs a stop-the-world cycle and reclaims shareable and cross-Ractor garbage. This option, although it defaults to true, is ignored unless full_mark, immediate_mark and immediate_sweep are all true. 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 default GC.

    • false: collect only the calling Ractor’s own objects. Shareable objects and cross-ractor garbage are not swept.

    GC.stat counts a local major collection under :major_gc_count, a local minor collection under :minor_gc_count, and a global collection under :global_gc_count for the calling Ractor.

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

  1. 4.1
    • 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.