Memory Leaks: How to see GC Roots?

Hello!

Is there a way to get a list of all GC roots in my Ruby program? I know
there is a memory leak somewhere in my application, I just don’t know in
which part. Listing all variables on the Heap becomes too large, but
just
seeing the GC roots would be a great help. Can you suggest some ideas?

Best wishes,

Sven C. Koehler

Sven C. Koehler wrote:

Hello!

Is there a way to get a list of all GC roots in my Ruby program? I
know there is a memory leak somewhere in my application, I just don’t
know in which part. Listing all variables on the Heap becomes too
large, but just seeing the GC roots would be a great help. Can you
suggest some ideas?

I don’t think this info is available. But how about statistics per
class?
That way you can at least determine which class is responsible for the
massive object count.

17:09:39 [source]: ruby -r pp -e ‘c=Hash.new(0); ObjectSpace.each_object
{|obj| c[obj.class]+=1}; pp c.sort_by {|k,v| -v}’
[[String, 582],
[Class, 185],
[Module, 18],
[Array, 8],
[Float, 6],
[Regexp, 6],
[Object, 3],
[IO, 3],
[File, 2],
[NoMemoryError, 1],
[SystemStackError, 1],
[Binding, 1],
[fatal, 1],
[ThreadGroup, 1],
[Hash, 1],
[Thread, 1]]

Kind regards

robert

Sven C. Koehler wrote:

Hello!

Is there a way to get a list of all GC roots in my Ruby program? I know
there is a memory leak somewhere in my application, I just don’t know in
which part. Listing all variables on the Heap becomes too large, but just
seeing the GC roots would be a great help. Can you suggest some ideas?

Best wishes,

Sven C. Koehler

There’s a ruby patch to show what objects are reachable from the roots
(and the chain of references in each case):

http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/151854?151368-152549

It should easy to modify it to just show the roots.

On Fri, Dec 02, 2005 at 06:18:57AM +0900, Joel VanderWerf wrote:

There’s a ruby patch to show what objects are reachable from the roots
(and the chain of references in each case):

http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/151854?151368-152549

It should easy to modify it to just show the roots.

I tried to use the patch with Ruby 1.8.3 and the current CVS Trunk
snapshot as of today (2005-12-02). With Ruby 1.8.3 I had to adjust the
rb_sprintf calls to use snprintf. In order to use the patch with the
current Trunk, I had to apply part of the patch by hand. However, I got
no usable output by trying to use it on both versions, so I don’t know
whether the patch is actually able to access the GC roots.

The output looks on both ruby 1.8.3 and the CVS Trunk as this:

irb(main):001:0> $a = File.open(“fo”, “w”)
=> #<File:fo>
irb(main):002:0> $b = [$a]
=> [#<File:fo>]
irb(main):003:0> GC.reachability_paths($a)
(irb):3: warning: Checking frame stack…
(irb):3: warning: …found, after 0 steps!
(irb):3: warning: …found, after 0 steps!
(irb):3: warning: Checking ruby_class…
(irb):3: warning: Checking ruby_scope…
(irb):3: warning: Checking save_regs_gc_mark…
(irb):3: warning: Checking stack_start…
(irb):3: warning: …found, after 0 steps!
(irb):3: warning: …found, after 0 steps!
(irb):3: warning: …found, after 0 steps!
(irb):3: warning: …found, after 0 steps!
(irb):3: warning: …found, after 0 steps!
(irb):3: warning: …found, after 0 steps!
(irb):3: warning: …found, after 0 steps!
(irb):3: warning: …found, after 0 steps!
(irb):3: warning: Checking threads…
(irb):3: warning: Checking C globals…
(irb):3: warning: Checking end_proc…
(irb):3: warning: Checking global_tbl…
(irb):3: warning: Checking class_tbl…
(irb):3: warning: …found, after 0 steps!
(irb):3: warning: Checking trap_list…
(irb):3: warning: Checking generic_ivar_tbl…
(irb):3: warning: Checking mark parser…
(irb):3: warning: Checking mark stack…
(irb):3: warning: Unmarking…
(irb):3: warning: Done.
=> [[], [], [], [], [], [], [], [], [], [], []]

I would have expected $a to be reachable via $b.

Best wishes,

Sven C. Koehler

In message [email protected], Sven C. Koehler
[email protected] writes

Is there a way to get a list of all GC roots in my Ruby program? I know
there is a memory leak somewhere in my application, I just don’t know in
which part. Listing all variables on the Heap becomes too large, but just
seeing the GC roots would be a great help. Can you suggest some ideas?

If you are on Windows then Ruby Memory Validator can do this.

    http://www.softwareverify.com

Stephen