What's the point: FREE in ObjectSpace.count_objects on ruby ​​MRI

I would like to know what the count associated with the key :FREE

was returning from ObjectSpace.count_object

. The documentation says that this hash is implementation specific, so my question is specific to the MRI 2.1 ruby.

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

.

Any ideas?

In some cases, the free account far exceeds the number of properties available through ObjectSpace.each_object

, and hence I don't seem to have any information about them. They take up memory. In my program, the count :FREE

is high even after starting garbage collection.

+3


source to share


1 answer


We can find the value :FREE

directly 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 check if there is a funny case: 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 in the heap that have not been used.

+1


source







All Articles