Forum: Ruby Confusing array garbage collection, how to get valid heap sn

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
057dda316e3e39d6266bcc5f0fa2e50e?d=identicon&s=25 Greg Fodor (Guest)
on 2007-05-23 03:00
(Received via mailing list)
We are working on tracking down a memory leak, and are hoping to use
heap snapshotting to find the culprit. Using the neat bleak_house

I have got snapshotting working on my local machine. Note this library
does not go through ObjectSpaces, but uses a custom ruby build, so its
much more accurate in showing the true state of the heap. However,
there seems to be a bit of a problem, this simple script I wrote for
testing purposes:

# Snapshot here
a = { }
a = nil
1.upto(20) { GC.start; ObjectSpace.garbage_collect; sleep 1; }
# Snapshot here

Shows that the 1000 objects in the array are still on the heap in the
second snapshot. Now, I realize GC.start makes no guarantees about
actually removing the objects marked, but it seems that in this case
they should go away since I am calling it so aggressively over such a
long period. What is really strange however is that if I all a.clear
before setting a to nil, the objects *do* go away upon the first
GC.start. So, either there is something still holding those references
in the above case, or the GC is extraordinarily conservative in
relation to GC.start.

I've also tried wrapping this code up into a class, putting the array
into an instance variable, and I get the same behavior. (This was to
make sure there are no procs or something at the root scope holding
these guys.)

In order to effectively track down memory leaks, there needs to be the
concept of a totally GC'ed heap snapshot. It appears based upon the
above behavior this is difficult to get. If the above code was under
test my heap snapshot would show a 'leak' of 1000 objects even though
my code was correctly dereferencing them. Is there something I am
missing here?
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2007-05-23 03:57
(Received via mailing list)
Greg Fodor wrote:
> testing purposes:
> # Snapshot here
> a = { }
> a = nil
> 1.upto(20) { GC.start; ObjectSpace.garbage_collect; sleep 1; }
> # Snapshot here

This is probably not very helpful, because I don't know what bleak_house
is but:

- calling GC repeatedly is unlikely to make a difference

- GC is conservative in treating stack data as potential pointers

- you can see the effect in the following example:

n0 = ObjectSpace.each_object(Object){}

def foo
   a = { }
   a = nil

   a = { }
   a = nil

   a = { }
   a = nil


n1 = ObjectSpace.each_object(Object){}

p n1 - n0


This outputs 998 for me. If you only create one array inside of #foo,
the output is the same, probably since the stack space was reused.
This topic is locked and can not be replied to.