Read GC information

irb(main):002:0> pp GC.stat; nil {:count=>7,
:heap_used=>77,
:heap_length=>77,
:heap_increment=>0,
:heap_live_num=>14208,
:heap_free_num=>17146,
:heap_final_num=>104}
=> nil

count

The number of times GC ran.

heap_used

  • The number of heap pages allocated.
  • The larger this number is, the more memory our Ruby process consumes, and the more work GC has to do.
  • To estimate the Ruby heap memory consumption, multiply this by HEAP_OBJ_LIMIT and sizeof(RVALUE).
  • In our example, the memory used for heap is 77 * 408 * 40 = 1,256,640 bytes, about 1.2 MB.
  • The name of this parameter is misleading. It doesn’t necessarily mean that all of these heap pages are used. They might be allocated, but empty. Also, this number isn’t cumulative. It can be decreased if Ruby shrinks the heap space.

heap_increment

  • In theory, this should be the number of heap pages that can be allocated before the interpreter needs to run the GC and grow the heap space again.
  • In practice, this number is always 0 in Ruby 1.9.

heap_length

  • The total number of heap pages, including heap_used and heap_increment. heap_live_num
  • The current number of live objects in the heap.
  • This number includes objects that are still live but will be collected next time GC runs. So we can’t use it to estimate the number of free slots in the heap.

heap_free_num

  • The current number of free object slots in the heap after the last GC run.
  • This number is also misleading and can’t be used to estimate the current number of free slots. The only way to know how much free space you have on the heap is to call GC.start before looking at the heap_free_num.

heap_final_num

  • The number of objects that weren’t finalized during the last GC and that will be finalized later.