Memory Inspector

Eric Hodel | Wed, 30 Aug 2006 08:13:00 GMT

Posted in ,

I've hacked into gc.c and added an alternative to ObjectSpace.each_object that gives you the contents of every heap slot in Ruby's memory space.

$ ./ruby -I ~/Work/p4/zss/src/RubyInline/dev/ ~/heapwalk.rb
[id, size, inspect]
0x001aa004 0x00000000                                                    :__free
0x001aa018 0x00000000                                                    :__free
0x001aa02c 0x00000000                                                    :__free
0x001aa040 0x00000000                                                    :__free
[the beginning is boring...]
0x001caa20 0x00000014                                           #<Class:Marshal>
0x001caa34 0x00000014                                                    :__node
0x001caa48 0x00000014                                                    :__node
0x001caa5c 0x0000000c                                                    Marshal
[but there's things like nodes, singleton classes, modules,]
0x001caae8 0x0000000c                #<NoMemoryError: failed to allocate memory>
0x001caafc 0x0000002d                                "failed to allocate memory"
0x001cab10 0x00000014                                                         []
[exceptions, strings, arrays and other objects,]
0x001ce5f8 0x00000014                                                  :__iclass
[included Modules,]
0x001dad30 0x00000014                                                   :__scope
[scopes, and some other stuff that's too hard to find]

It requires a hack to gc.c to gain access to the heap, but combined with Ryan's memory visualizer this might generate something usable.

Code will be available later I've audited my object size calculations (and have slept). 3 comments

Comments RSS FEED

Wow! there seems to be a lot of work around the ‘Net (and especially in the Seattle.rb) on dealing with Ruby’s memory.

I’m hoping more of this stuff gets tied together into nice useable packages to help improve Ruby and our Ruby programs.

Thanks for the hard work Eric.

Pat Eyler said about 5 hours later

Eric,

This is great. Having visibility is the first step towards fixing anything.

Do you think it’s possible to create a module to load into any RoR app to dump out the heap size?

Would love to see you code…

Good work.

David Koe said 14 days later

@David: You could build one simply with mem_inspect which has the size of the objects and everything. It works similar to ObjectSpace::each_object, but includes (a good guess at) the size of the object.

Eric Hodel said 14 days later

Comments are disabled