From: Thierry Lambert on
Minasan konbanha

I have a question about garbage collection. In short, I thought that
releasing any reference to an object, and then calling GC.start would
garbage-collect the object. It turns out not to be true (on Windoze),
and I've been scratching my head to understand what's going on (in
vain).

Here's the problem:

class Test
end

class Inner < Test
end

class Outer < Test
def initialize
@inner = Inner.new
end
def free
@inner = nil
end
end

o = Outer.new
o.free
GC.start
# Now inner object should be gone, right? Wrong:
ObjectSpace.each_object(Test) { |o| puts o.inspect }
# => #<Inner:0x40b4470>
#<Outer:0x40b4ff8 @inner=nil>

I'm using ruby 1.8.7p249, but I have the same results with 1.9.1p378.
I'm on windoze (yeah, I know...). The problem does not appear on my Mac
(Inner is gone after GC). Any idea about this, a workaround? I need to
free OLE automation objects, that's why...

Cheers
-- Thierry
--
Posted via http://www.ruby-forum.com/.

From: Robert Klemme on
On 04.07.2010 18:36, Thierry Lambert wrote:
> I have a question about garbage collection. In short, I thought that
> releasing any reference to an object, and then calling GC.start would
> garbage-collect the object. It turns out not to be true (on Windoze),
> and I've been scratching my head to understand what's going on (in
> vain).

There is no guarantee for GC. For efficiency reasons most GC
implementations do not simply delete all objects once there is no life
reference to them any more. Even if you explicitly start a collection
there is no guarantee.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
From: Thierry Lambert on
Robert Klemme wrote:
> There is no guarantee for GC. For efficiency reasons most GC
> implementations do not simply delete all objects once there is no life
> reference to them any more. Even if you explicitly start a collection
> there is no guarantee.
Thanks Robert, I kind of had this reservation in the back of my head, so
I'm more looking for a workaround: in real life, the Inner object would
be an application started through OLE automation, for instance Excel,
Word, Access... The problem is that the application won't close after
sending it a Quit: it will close after being garbage-collected. Any idea
how I can control the fact that WIN32OLE objects are actually freed?
(ole_free does not work well, and I won't complain since the doc says we
should not use it...)
--
Posted via http://www.ruby-forum.com/.

From: Robert Klemme on
2010/7/5 Thierry Lambert <thyresias(a)gmail.com>:
> Robert Klemme wrote:
>> There is no guarantee for GC.  For efficiency reasons most GC
>> implementations do not simply delete all objects once there is no life
>> reference to them any more.  Even if you explicitly start a collection
>> there is no guarantee.
> Thanks Robert, I kind of had this reservation in the back of my head, so
> I'm more looking for a workaround: in real life, the Inner object would
> be an application started through OLE automation, for instance Excel,
> Word, Access... The problem is that the application won't close after
> sending it a Quit: it will close after being garbage-collected. Any idea
> how I can control the fact that WIN32OLE objects are actually freed?
> (ole_free does not work well, and I won't complain since the doc says we
> should not use it...)

There is generally no way to control when an object will be collected
unless you start hacking the interpreter. For proper resource
deallocation you usually use begin - ensure, but this can only work by
invoking methods not forcing destruction of objects. See also

http://blog.rubybestpractices.com/posts/rklemme/002_Writing_Block_Methods.html

I don't know WIN32OLE so I can't tell whether there is another trick
that helps, sorry.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

From: Roger Pack on
> ObjectSpace.each_object(Test) { |o| puts o.inspect }
> # => #<Inner:0x40b4470>
> #<Outer:0x40b4ff8 @inner=nil>
>
> I'm using ruby 1.8.7p249, but I have the same results with 1.9.1p378.
> I'm on windoze (yeah, I know...). The problem does not appear on my Mac
> (Inner is gone after GC). Any idea about this, a workaround? I need to
> free OLE automation objects, that's why...

Yeah get the same thing here. It's the nature of conservative GC.
I did a little writeup of it here:

http://en.wikibooks.org/wiki/Ruby_Programming/Reference/Objects/GC#Conservative

-r
--
Posted via http://www.ruby-forum.com/.