Skip to content
class method

GC.stat

(?::Hash[::Symbol, untyped]? hash) -> ::Hash[::Symbol, untyped]
(::Symbol key) -> ::Integer
GC.stat → new_hash
GC.stat(key) → value
GC.stat(hash) → hash

This method is implementation-specific to CRuby.

Returns GC statistics. The particular statistics are implementation-specific and may change in the future without notice.

With no argument given, returns information about the most recent garbage collection:

Example
GC.stat
# =>
{count: 28,
 time: 1,
 marking_time: 1,
 sweeping_time: 0,
 heap_allocated_pages: 521,
 heap_empty_pages: 0,
 heap_allocatable_slots: 0,
 heap_available_slots: 539590,
 heap_live_slots: 422243,
 heap_free_slots: 117347,
 heap_final_slots: 0,
 heap_marked_slots: 264877,
 heap_eden_pages: 521,
 total_allocated_pages: 521,
 total_freed_pages: 0,
 total_allocated_objects: 2246376,
 total_freed_objects: 1824133,
 malloc_increase_bytes: 50982,
 malloc_increase_bytes_limit: 18535172,
 minor_gc_count: 18,
 major_gc_count: 10,
 compact_count: 0,
 read_barrier_faults: 0,
 total_moved_objects: 0,
 remembered_wb_unprotected_objects: 0,
 remembered_wb_unprotected_objects_limit: 2162,
 old_objects: 216365,
 old_objects_limit: 432540,
 oldmalloc_increase_bytes: 1654232,
 oldmalloc_increase_bytes_limit: 16846103}

With symbol argument key given, returns the value for that key:

Example
GC.stat(:count) # => 30

With hash argument hash given, returns that hash with GC statistics merged into its content; this form may be useful in minimizing probe effects:

Example
h = {foo: 0, bar: 1}
GC.stat(h)
h.keys.take(5) # => [:foo, :bar, :count, :time, :marking_time]

The hash includes entries such as:

  • :count: The total number of garbage collections run since application start (count includes both minor and major garbage collections).

  • :time: The total time spent in garbage collections (in milliseconds).

  • :heap_allocated_pages: The total number of allocated pages.

  • :heap_empty_pages: The number of pages with no live objects, and that could be released to the system.

  • :heap_sorted_length: The number of pages that can fit into the buffer that holds references to all pages.

  • :heap_allocatable_pages: The total number of pages the application could allocate without additional GC.

  • :heap_available_slots: The total number of slots in all :heap_allocated_pages.

  • :heap_live_slots: The total number of slots which contain live objects.

  • :heap_free_slots: The total number of slots which do not contain live objects.

  • :heap_final_slots: The total number of slots with pending finalizers to be run.

  • :heap_marked_slots: The total number of objects marked in the last GC.

  • :heap_eden_pages: The total number of pages which contain at least one live slot.

  • :total_allocated_pages: The cumulative number of pages allocated since application start.

  • :total_freed_pages: The cumulative number of pages freed since application start.

  • :total_allocated_objects: The cumulative number of objects allocated since application start.

  • :total_freed_objects: The cumulative number of objects freed since application start.

  • :malloc_increase_bytes: Amount of memory allocated on the heap for objects. Decreased by any GC.

  • :malloc_increase_bytes_limit: When :malloc_increase_bytes crosses this limit, GC is triggered.

  • :minor_gc_count: The total number of minor garbage collections run since process start.

  • :major_gc_count: The total number of major garbage collections run since process start.

  • :compact_count: The total number of compactions run since process start.

  • :read_barrier_faults: The total number of times the read barrier was triggered during compaction.

  • :total_moved_objects: The total number of objects compaction has moved.

  • :remembered_wb_unprotected_objects: The total number of objects without write barriers.

  • :remembered_wb_unprotected_objects_limit: When :remembered_wb_unprotected_objects crosses this limit, major GC is triggered.

  • :old_objects: Number of live, old objects which have survived at least 3 garbage collections.

  • :old_objects_limit: When :old_objects crosses this limit, major GC is triggered.

  • :oldmalloc_increase_bytes: Amount of memory allocated on the heap for objects. Decreased by major GC.

  • :oldmalloc_increase_bytes_limit: When :oldmalloc_increase_bytes crosses this limit, major GC is triggered.

Ruby version changes

  1. 3.1
    • “GC.measure_total_time = true” enables the measurement of GC. Measurement can introduce overhead. It is enabled by default. GC.measure_total_time returns the current setting. GC.stat[:time] or GC.stat(:time) returns measured time in milli-seconds. [[Feature #10917]]

See also .start .stat_heap .measure_total_time= .stress also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0