How to find the memory used by ruby ​​object?

Is there any function in ruby ​​to find the memory used by the ruby ​​object.
Like C has a function sizeof() , and PHP has a function memory_get_usage() . Does ruby ​​have an equivalent function / method?

+7
source share
1 answer

This is a stretch, but if your goal is to look for a memory leak, and not see the size of individual objects, you can look at object_count(cls) , as in:

 >> ObjectSpace.each_object(Object).count => 114629 >> ObjectSpace.each_object(Array).count => 10209 

etc .. FWIW, the characters are a little different: you can get the number of characters through:

 >> Symbol.all_symbols.count => 17878 

To find out if you have a leak, you can manually call GC, count your objects, run the code for a while, call GC again, and then see if the number of objects has increased.

Of course, this does not tell you about the size of each object, how much stands out from each class.

There is also memprof , but I admit that I have not used it yet.

+2
source

All Articles