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
GCbuild options
Public Class Methods
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;
}
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
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
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
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
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
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
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
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);
}
Returns the number of malloc() allocations.