3

I would like to know what the count associated with the key :FREE returned from ObjectSpace.count_object. The documentation says this hash is implementation-specific, and so my question refers specifically to MRI ruby 2.1.

There have been at least two questions about this (here and here), but no answers regarding :FREE.

Any ideas?

In some cases, the free count is much higher than the number of objects accessible through ObjectSpace.each_object and thus I don't seem to have any information about them. Are they taking up memory. In my program the :FREE count is high even after running garbage collection.

Community
  • 1
  • 1
Kevin Bullaughey
  • 2,556
  • 25
  • 37
  • 1
    As I recall, MRI's GC doesn't actually free allocated objects when it cleans up. Instead it collects the unused object in a linked list. These objects are recycled into new objects as needed. This reduces the amount of actual allocation/freeing that needs to be done. I would guess that `:FREE` is the size of this linked list. – Max Nov 12 '14 at 00:22

1 Answers1

2

We can find the meaning of :FREE straight from the implementation itself (from gc.c )

*  The keys starting with +:T_+ means live objects.
*  For example, +:T_ARRAY+ is the number of arrays.
*  +:FREE+ means object slots which is not used now.
*  +:TOTAL+ means sum of above.

Then we can take a look at the tests for it (from test_gc.rb) :

assert_equal(count[:TOTAL]-count[:FREE], stat[:heap_live_slots])
assert_equal(count[:FREE], stat[:heap_free_slots])

And finally, we can double check there's no funny business going on with: GC.stat[:heap_free_slot] == ObjectSpace.count_objects[:FREE]

irb(main):001:0> GC.stat[:heap_free_slot] == ObjectSpace.count_objects[:FREE] => true

So, :FREE indicates the number of allocated slots on the heap that haven't been used.

Thai Wood
  • 505
  • 4
  • 13