module 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.

Constants

INTERNAL_CONSTANTS

internal constants

OPTS

GC build options

Public Class Methods

add_stress_to_class(class[, ...]) click to toggle source

Raises NoMemoryError when allocating an instance of the given classes.

static VALUE
rb_gcdebug_add_stress_to_class(int argc, VALUE *argv, VALUE self)
{
    rb_objspace_t *objspace = &rb_objspace;

    if (!stress_to_class) {
        stress_to_class = rb_ary_tmp_new(argc);
    }
    rb_ary_cat(stress_to_class, argv, argc);
    return self;
}
auto_compact → true or false click to toggle source

Returns whether or not automatic compaction has been enabled.

# File ruby_3_1_6/gc.rb, line 46
def self.auto_compact
  Primitive.gc_get_auto_compact
end
auto_compact = flag click to toggle source

Updates automatic compaction mode.

When enabled, the compactor will execute on every major collection.

Enabling compaction will degrade performance on major collections.

# File ruby_3_1_6/gc.rb, line 58
def self.auto_compact=(flag)
  Primitive.gc_set_auto_compact(flag)
end
compact click to toggle source

This function compacts objects together in Ruby’s heap. It eliminates unused space (or fragmentation) in the heap by moving objects in to that unused space. This function returns a hash which contains statistics about which objects were moved. See ‘GC.latest_gc_info` for details about compaction statistics.

This method is implementation specific and not expected to be implemented in any implementation besides MRI.

# File ruby_3_1_6/gc.rb, line 238
def self.compact
  Primitive.gc_compact
end
count → Integer click to toggle source

The number of times GC occurred.

It returns the number of times GC occurred since the process started.

# File ruby_3_1_6/gc.rb, line 120
def self.count
  Primitive.gc_count
end
disable → true or false click to toggle source

Disables garbage collection, returning true if garbage collection was already disabled.

GC.disable   #=> false
GC.disable   #=> true
# File ruby_3_1_6/gc.rb, line 84
def self.disable
  Primitive.gc_disable
end
enable → true or false click to toggle source

Enables garbage collection, returning true if garbage collection was previously disabled.

GC.disable   #=> false
GC.enable    #=> true
GC.enable    #=> false
# File ruby_3_1_6/gc.rb, line 72
def self.enable
  Primitive.gc_enable
end
latest_compact_info -> {:considered=>{:T_CLASS=>11}, :moved=>{:T_CLASS→11}} click to toggle source
Returns information about object moved in the most recent GC compaction.

The returned hash has two keys :considered and :moved. The hash for :considered lists the number of objects that were considered for movement by the compactor, and the :moved hash lists the number of objects that were actually moved. Some objects can’t be moved (maybe they were pinned) so these numbers can be used to calculate compaction efficiency.

# File ruby_3_1_6/gc.rb, line 223
def self.latest_compact_info
  Primitive.gc_compact_stats
end
latest_gc_info -> {:gc_by→:newobj} click to toggle source
latest_gc_info(hash) → hash
latest_gc_info(:major_by) → :malloc
Returns information about the most recent garbage collection.

If the optional argument, hash, is given, it is overwritten and returned. This is intended to avoid probe effect.

# File ruby_3_1_6/gc.rb, line 209
def self.latest_gc_info hash_or_key = nil
  Primitive.gc_latest_gc_info hash_or_key
end
malloc_allocated_size → Integer click to toggle source

Returns the size of memory allocated by malloc().

Only available if ruby was built with CALC_EXACT_MALLOC_SIZE.

static VALUE
gc_malloc_allocated_size(VALUE self)
{
    return UINT2NUM(rb_objspace.malloc_params.allocated_size);
}
malloc_allocations → Integer click to toggle source

Returns the number of malloc() allocations.