# Memory Leaks: How to see GC Roots?

**URL:** <https://www.ruby-forum.com/t/memory-leaks-how-to-see-gc-roots/49225>\
**Category:** Ruby\
**Created:** [December 1, 2005, 5:02pm UTC](https://www.ruby-forum.com/t/memory-leaks-how-to-see-gc-roots/49225 "2005-12-01T17:02:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![schween](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@schween](https://www.ruby-forum.com/u/schween)\
**Post date:** [December 1, 2005, 5:02pm UTC](https://www.ruby-forum.com/t/memory-leaks-how-to-see-gc-roots/49225/1 "2005-12-01T17:02:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![schween](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@schween](https://www.ruby-forum.com/u/schween)\
**Post date:** [December 1, 2005, 5:14pm UTC](https://www.ruby-forum.com/t/memory-leaks-how-to-see-gc-roots/49225/2 "2005-12-01T17:14:17Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![schween](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@schween](https://www.ruby-forum.com/u/schween)\
**Post date:** [December 1, 2005, 10:19pm UTC](https://www.ruby-forum.com/t/memory-leaks-how-to-see-gc-roots/49225/3 "2005-12-01T22:19:12Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![schween](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@schween](https://www.ruby-forum.com/u/schween)\
**Post date:** [December 2, 2005, 5:37pm UTC](https://www.ruby-forum.com/t/memory-leaks-how-to-see-gc-roots/49225/4 "2005-12-02T17:37:08Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![schween](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@schween](https://www.ruby-forum.com/u/schween)\
**Post date:** [December 2, 2005, 6:25pm UTC](https://www.ruby-forum.com/t/memory-leaks-how-to-see-gc-roots/49225/5 "2005-12-02T18:25:53Z")

</div>

In message [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid), Sven C. Koehler  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
