# How to understand why object is not collected?

**URL:** <https://www.ruby-forum.com/t/how-to-understand-why-object-is-not-collected/73988>\
**Category:** Ruby\
**Created:** [October 28, 2006, 8:27pm UTC](https://www.ruby-forum.com/t/how-to-understand-why-object-is-not-collected/73988 "2006-10-28T20:27:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor\_SZverokSS\_Sh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@Victor\_SZverokSS\_Sh](https://www.ruby-forum.com/u/Victor_SZverokSS_Sh)\
**Post date:** [October 28, 2006, 8:27pm UTC](https://www.ruby-forum.com/t/how-to-understand-why-object-is-not-collected/73988/1 "2006-10-28T20:27:48Z")

</div>

Hi all.

Here’s a problem.

I have some object of MyClass with complex “freeing” logic, and I want  
to  
test this logic.  
I do:

* * *

span = … #somehow created object

ObjectSpace.define\_finalizer(span, proc{p “gc ok”}) #define test  
finalizer

span.detach #remove object from all of my internal caches, no  
references  
to object should exist

span = nil #remove the last reference to object

GC.start #\<===HERE the object must be collected and finalized

## p “end of code”

## I expect:

## “gc ok” “end of code”

## I receive:

## “end of code” “gc ok”

OK, I suppose there are some references to the object still exists. I  
want  
to know, at least, what number of references still exists (in ideal, I’d  
want to know, where they are).  
How I can? (except of harcode hacking GC’s C code)

Ruby 1.9 (downloaded a week ago), Windows.

Thanks.

V.

---

<div class="post-metadata">

**Author:** ![Victor\_SZverokSS\_Sh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@Victor\_SZverokSS\_Sh](https://www.ruby-forum.com/u/Victor_SZverokSS_Sh)\
**Post date:** [October 28, 2006, 9:53pm UTC](https://www.ruby-forum.com/t/how-to-understand-why-object-is-not-collected/73988/2 "2006-10-28T21:53:55Z")

</div>

Victor “Zverok” Shepelev wrote:

> ObjectSpace.define\_finalizer(span, proc{p “gc ok”}) #define test finalizer
> 
> span.detach #remove object from all of my internal caches, no references  
> to object should exist
> 
> span = nil #remove the last reference to object
> 
> GC.start #\<===HERE the object must be collected and finalized

This is probably a situation where GC is being conservative. A simple  
example:

class Foo; end

foo = Foo.new  
foo = nil

GC.start

p ObjectSpace.each\_object(Foo) {} # ==\> 1

Probably this means that the Foo instance pointer is still visible  
somewhere in the local frame.

But that doesn’t mean that there is a leak:

class Foo; end

10.times do  
Foo.new  
end

GC.start

p ObjectSpace.each\_object(Foo) {} # ==\> 1

In my experience, it’s hard to write precise unit tests for GC behavior,  
and you end up with some fuzziness. The important property of GC is  
really the asymptotic behavior.

---

<div class="post-metadata">

**Author:** ![Victor\_SZverokSS\_Sh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@Victor\_SZverokSS\_Sh](https://www.ruby-forum.com/u/Victor_SZverokSS_Sh)\
**Post date:** [October 28, 2006, 10:21pm UTC](https://www.ruby-forum.com/t/how-to-understand-why-object-is-not-collected/73988/3 "2006-10-28T22:21:58Z")

</div>

On 2006.10.29 03:26, Victor Zverok S. wrote:

> Hi all.
> 
> Here’s a problem.
> 
> I have some object of MyClass with complex “freeing” logic, and I want to  
> test this logic.

Hard to say what is going wrong but the solution is simple. Whenever  
you have a resource that must be released, do it explicitly. The way  
this is usually implemented is either:

f = File.open ‘foo’  
puts f.read  
f.close

Or, more conveniently

File.open(‘foo’) {|f| puts f.read}

The block-form uses ensure to make sure the file handle gets closed.

---

<div class="post-metadata">

**Author:** ![Victor\_SZverokSS\_Sh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@Victor\_SZverokSS\_Sh](https://www.ruby-forum.com/u/Victor_SZverokSS_Sh)\
**Post date:** [October 28, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/how-to-understand-why-object-is-not-collected/73988/4 "2006-10-28T21:59:56Z")

</div>

From: Joel VanderWerf [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
Sent: Saturday, October 28, 2006 10:52 PM

> p ObjectSpace.each\_object(Foo) {} # ==\> 1  
> OK, I’ve got it.

> In my experience, it’s hard to write precise unit tests for GC behavior,  
> and you end up with some fuzziness. The important property of GC is  
> really the asymptotic behavior.

Just for now, I need no precise unit test. I just want to know, would  
the  
objects of MyClass collected in principle. Can I achieve the goal  
somehow?

V.

---

<div class="post-metadata">

**Author:** ![Victor\_SZverokSS\_Sh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@Victor\_SZverokSS\_Sh](https://www.ruby-forum.com/u/Victor_SZverokSS_Sh)\
**Post date:** [October 28, 2006, 10:27pm UTC](https://www.ruby-forum.com/t/how-to-understand-why-object-is-not-collected/73988/5 "2006-10-28T22:27:58Z")

</div>

Victor “Zverok” Shepelev wrote:

> > GC.start  
> > objects of MyClass collected in principle. Can I achieve the goal somehow?  
> > What about creating a lot of them in a loop, without keeping references?  
> > Then I would expect GC to leave at most one instance.

---

<div class="post-metadata">

**Author:** ![Victor\_SZverokSS\_Sh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@Victor\_SZverokSS\_Sh](https://www.ruby-forum.com/u/Victor_SZverokSS_Sh)\
**Post date:** [October 28, 2006, 10:23pm UTC](https://www.ruby-forum.com/t/how-to-understand-why-object-is-not-collected/73988/6 "2006-10-28T22:23:58Z")

</div>

From: Eero S. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
Sent: Saturday, October 28, 2006 11:20 PM

> you have a resource that must be released, do it explicitly. The way  
> The block-form uses ensure to make sure the file handle gets closed.  
> I know this (I was C++ guy foe many years, so RAII is my breath), but  
> can’t  
> use.  
> My objects are HTML DOM elements and I want to be sure they are GC’d  
> when  
> detached from DOM and have no other references.

V.

---

<div class="post-metadata">

**Author:** ![Victor\_SZverokSS\_Sh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@Victor\_SZverokSS\_Sh](https://www.ruby-forum.com/u/Victor_SZverokSS_Sh)\
**Post date:** [October 28, 2006, 10:39pm UTC](https://www.ruby-forum.com/t/how-to-understand-why-object-is-not-collected/73988/7 "2006-10-28T22:39:59Z")

</div>

From: Joel VanderWerf [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
Sent: Saturday, October 28, 2006 11:26 PM

> > > 
> > 
> > Just for now, I need no precise unit test. I just want to know, would the  
> > objects of MyClass collected in principle. Can I achieve the goal  
> > somehow?
> 
> What about creating a lot of them in a loop, without keeping references?  
> Then I would expect GC to leave at most one instance.

They can’t be created by .new, they created in correspondence to some  
HTML  
DOM by C extension.  
Problem already solved by the trick:

–  
def test  
span = …  
ObjectSpace.define\_finalizer(span, proc{p “gc ok”})  
span.detach  
span = nil  
end

test

GC.start

## p “end of code”

Output corresponds to my expectations (gc ok =\> end of code)

Thanks for your help!

V.
