Maintenance of Ruby 2.0.0 ended on February 24, 2016. Read more

In Files

  • gc.c
toggle debugging


The GC profiler provides access to information on GC runs including time, length and object space size.



require 'rdoc/rdoc'


See also GC.count, GC.malloc_allocated_size and GC.malloc_allocations

Public Class Methods

GC::Profiler.clear → nil click to toggle source

Clears the GC profiler data.

               static VALUE
    rb_objspace_t *objspace = &rb_objspace;

    if (GC_PROFILE_RECORD_DEFAULT_SIZE * 2 < objspace->profile.size) {
        objspace->profile.size = GC_PROFILE_RECORD_DEFAULT_SIZE * 2;
        objspace->profile.record = realloc(objspace->profile.record, sizeof(gc_profile_record) * objspace->profile.size);
        if (!objspace->profile.record) {
    MEMZERO(objspace->profile.record, gc_profile_record, objspace->profile.size);
    objspace->profile.count = 0;
    return Qnil;
GC::Profiler.disable → nil click to toggle source

Stops the GC profiler.

               static VALUE
    rb_objspace_t *objspace = &rb_objspace;

    objspace-> = FALSE;
    return Qnil;
GC::Profiler.enable → nil click to toggle source

Starts the GC profiler.

               static VALUE
    rb_objspace_t *objspace = &rb_objspace;

    objspace-> = TRUE;
    return Qnil;
GC::Profiler.enabled? → true or false click to toggle source

The current status of GC profile mode.

               static VALUE
gc_profile_enable_get(VALUE self)
    rb_objspace_t *objspace = &rb_objspace;
    return objspace-> ? Qtrue : Qfalse;
GC::Profiler.raw_data → [Hash, ...] click to toggle source

Returns an Array of individual raw profile data Hashes ordered from earliest to latest by :GC_INVOKE_TIME.

For example:

  # ...

The keys mean:


Time elapsed in seconds for this GC run


Time elapsed in seconds from startup to when the GC was invoked


Total bytes of heap used


Total size of heap in bytes


Total number of objects


Returns true if the GC is in mark phase

If ruby was built with GC_PROFILE_MORE_DETAIL, you will also have access to the following hash keys:

               static VALUE
    VALUE prof;
    VALUE gc_profile = rb_ary_new();
    size_t i;
    rb_objspace_t *objspace = (&rb_objspace);

    if (!objspace-> {
        return Qnil;

    for (i =0; i < objspace->profile.count; i++) {
        prof = rb_hash_new();
        rb_hash_aset(prof, ID2SYM(rb_intern("GC_TIME")), DBL2NUM(objspace->profile.record[i].gc_time));
        rb_hash_aset(prof, ID2SYM(rb_intern("GC_INVOKE_TIME")), DBL2NUM(objspace->profile.record[i].gc_invoke_time));
        rb_hash_aset(prof, ID2SYM(rb_intern("HEAP_USE_SIZE")), SIZET2NUM(objspace->profile.record[i].heap_use_size));
        rb_hash_aset(prof, ID2SYM(rb_intern("HEAP_TOTAL_SIZE")), SIZET2NUM(objspace->profile.record[i].heap_total_size));
        rb_hash_aset(prof, ID2SYM(rb_intern("HEAP_TOTAL_OBJECTS")), SIZET2NUM(objspace->profile.record[i].heap_total_objects));
        rb_hash_aset(prof, ID2SYM(rb_intern("GC_IS_MARKED")), objspace->profile.record[i].is_marked);
        rb_hash_aset(prof, ID2SYM(rb_intern("GC_MARK_TIME")), DBL2NUM(objspace->profile.record[i].gc_mark_time));
        rb_hash_aset(prof, ID2SYM(rb_intern("GC_SWEEP_TIME")), DBL2NUM(objspace->profile.record[i].gc_sweep_time));
        rb_hash_aset(prof, ID2SYM(rb_intern("ALLOCATE_INCREASE")), SIZET2NUM(objspace->profile.record[i].allocate_increase));
        rb_hash_aset(prof, ID2SYM(rb_intern("ALLOCATE_LIMIT")), SIZET2NUM(objspace->profile.record[i].allocate_limit));
        rb_hash_aset(prof, ID2SYM(rb_intern("HEAP_USE_SLOTS")), SIZET2NUM(objspace->profile.record[i].heap_use_slots));
        rb_hash_aset(prof, ID2SYM(rb_intern("HEAP_LIVE_OBJECTS")), SIZET2NUM(objspace->profile.record[i].heap_live_objects));
        rb_hash_aset(prof, ID2SYM(rb_intern("HEAP_FREE_OBJECTS")), SIZET2NUM(objspace->profile.record[i].heap_free_objects));
        rb_hash_aset(prof, ID2SYM(rb_intern("HAVE_FINALIZE")), objspace->profile.record[i].have_finalize);
        rb_ary_push(gc_profile, prof);

    return gc_profile;
    click to toggle source

Writes the ::result to $stdout or the given IO object.

               static VALUE
gc_profile_report(int argc, VALUE *argv, VALUE self)
    VALUE out;

    if (argc == 0) {
        out = rb_stdout;
    else {
        rb_scan_args(argc, argv, "01", &out);
    gc_profile_dump_on(out, rb_io_write);

    return Qnil;
GC::Profiler.result → String click to toggle source

Returns a profile data report such as:

GC 1 invokes.
Index    Invoke Time(sec)       Use Size(byte)     Total Size(byte)         Total Object                    GC time(ms)
    1               0.012               159240               212940                10647         0.00000000000001530000
               static VALUE
        VALUE str = rb_str_buf_new(0);
        gc_profile_dump_on(str, rb_str_buf_append);
        return str;
GC::Profiler.total_time → float click to toggle source

The total time used for garbage collection in seconds

               static VALUE
gc_profile_total_time(VALUE self)
    double time = 0;
    rb_objspace_t *objspace = &rb_objspace;
    size_t i;

    if (objspace-> && objspace->profile.count) {
        for (i = 0; i < objspace->profile.count; i++) {
            time += objspace->profile.record[i].gc_time;
    return DBL2NUM(time);

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit