5

In ruby 1.9.3, I'm using ObjectSpace to inspect the memory issue. The ObjectSpace.count_objects returns a hash, which looks like:

{:TOTAL=>1004232, :FREE=>258543, :T_OBJECT=>12519, :T_CLASS=>10318, :T_MODULE=>1330,    
:T_FLOAT=>2024, :T_STRING=>555422, :T_REGEXP=>3543, :T_ARRAY=>68372, :T_HASH=>5399,
:T_STRUCT=>542, :T_BIGNUM=>8105, :T_FILE=>10, :T_DATA=>44277, :T_MATCH=>6, :T_COMPLEX=>1,   
:T_RATIONAL=>82, :T_NODE=>31973, :T_ICLASS=>1766}

What does each hash value mean? And especially, why does the :TOTAL stay unchanged for a long time? Does it mean no new object is created?

I saw a similar posting, but no good answer yet.

Community
  • 1
  • 1
Bruce Lin
  • 2,700
  • 6
  • 28
  • 38
  • Possible duplicate of [On Ruby 1.9, what are the meanings of the hash keys when calling ObjectSpace.count\_objects (particularly :FREE, :T\_ICLASS, :T\_DATA, and :T\_NODE)?](https://stackoverflow.com/questions/3789929/on-ruby-1-9-what-are-the-meanings-of-the-hash-keys-when-calling-objectspace-cou) – Ilya Vassilevsky Mar 04 '18 at 18:01

1 Answers1

1
>> ObjectSpace.count_objects
=> {:TOTAL=>30205, :FREE=>131, :T_OBJECT=>1550, :T_CLASS=>921, :T_MODULE=>31, :T_FLOAT=>7, :T_STRING=>18368, :T_REGEXP=>185, :T_ARRAY=>4
196, :T_HASH=>254, :T_STRUCT=>1, :T_BIGNUM=>3, :T_FILE=>9, :T_DATA=>1311, :T_MATCH=>84, :T_COMPLEX=>1, :T_NODE=>3121, :T_ICLASS=>32}
>> class MyClass ; end
=> nil
>> ObjectSpace.count_objects
=> {:TOTAL=>30205, :FREE=>203, :T_OBJECT=>1562, :T_CLASS=>923, :T_MODULE=>31, :T_FLOAT=>7, :T_STRING=>18333, :T_REGEXP=>185, :T_ARRAY=>4
274, :T_HASH=>268, :T_STRUCT=>1, :T_BIGNUM=>3, :T_FILE=>9, :T_DATA=>1320, :T_MATCH=>97, :T_COMPLEX=>1, :T_NODE=>2956, :T_ICLASS=>32}
>> MyClass = nil
(irb):4: warning: already initialized constant MyClass
(irb):2: warning: previous definition of MyClass was here
=> nil
>> ObjectSpace.count_objects
=> {:TOTAL=>30205, :FREE=>87, :T_OBJECT=>1572, :T_CLASS=>923, :T_MODULE=>31, :T_FLOAT=>7, :T_STRING=>18425, :T_REGEXP=>185, :T_ARRAY=>43
39, :T_HASH=>279, :T_STRUCT=>1, :T_BIGNUM=>3, :T_FILE=>9, :T_DATA=>1328, :T_MATCH=>107, :T_COMPLEX=>1, :T_NODE=>2876, :T_ICLASS=>32}
>> MyClass
=> nil
>> GC.start
=> nil
>> ObjectSpace.count_objects
=> {:TOTAL=>30205, :FREE=>7356, :T_OBJECT=>1549, :T_CLASS=>921, :T_MODULE=>31, :T_FLOAT=>7, :T_STRING=>14100, :T_REGEXP=>184, :T_ARRAY=>
3821, :T_HASH=>244, :T_STRUCT=>1, :T_BIGNUM=>3, :T_FILE=>5, :T_DATA=>1285, :T_MATCH=>8, :T_COMPLEX=>1, :T_NODE=>657, :T_ICLASS=>32}
>> 

Looking at the T_CLASS object we can see that these are counts. When I remove the class and start the Garbage Collector it reduces the count again.

The TOTAL value remaining unchanged can not mean that there were no objects created, as obviously when you create a new object such as MyClass that count did not change. It could mean that it only reevaluates the total occasionally, not in real time.

But, with a little bit of math, when I sum the values, and remove the TOTAL count, I get TOTAL. So it looks like it is a count of the number of objects, which makes sense, when count_objects reports on the count of objects.

vgoff
  • 10,980
  • 3
  • 38
  • 56
  • Thanks vgoff, do you now what's the meaning of Total and Free? Also, if you are not creating hash or array, why the count of them keep changing? – Bruce Lin Jul 26 '13 at 23:22
  • Are you sure that methods you call don't create Array or Hash objects in their implementations? Free, I haven't looked at, perhaps it is a count that was freed and are ready for GC? Purely speculation though for FREE though. – vgoff Jul 26 '13 at 23:24
  • I mean in your example you only create a class right? Not array or hash, but they keep changing. Maybe GC is working? – Bruce Lin Jul 27 '13 at 01:19
  • Perhaps housekeeping and the act of reporting is creating those items. Even the object_count creates a hash just to report this information, right? This is going to require some code diving to get to the bottom of it, for sure. – vgoff Jul 27 '13 at 08:20