Forum: Ruby Memory Leaks: How to see GC Roots?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D71284e011fc1db85804d55515f7d5b8?d=identicon&s=25 schween (Guest)
on 2005-12-01 17:02
(Received via mailing list)
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
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 bob.news (Guest)
on 2005-12-01 17:14
(Received via mailing list)
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
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 vjoel (Guest)
on 2005-12-01 22:19
(Received via mailing list)
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/rub...

It should easy to modify it to just show the roots.
D71284e011fc1db85804d55515f7d5b8?d=identicon&s=25 schween (Guest)
on 2005-12-02 17:37
(Received via mailing list)
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/rub...
>
> 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
Dba0cb4cdad3b8e3b7ed0fddff5d20a5?d=identicon&s=25 snail (Guest)
on 2005-12-02 18:25
(Received via mailing list)
In message <20051201155717.GI20330@boing.buug.de>, Sven C. Koehler
<schween@snafu.de> 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
This topic is locked and can not be replied to.