bash-2.05b$ ruby d.rb
before call
before GC
"string"
recycled
after GC
after call
bash-2.05b$ expand -t2 d.rb
def test
x = 'string'
ObjectSpace.define_finalizer(x, proc { puts "recycled" })
p x
x = nil
end
f = lambda {
puts "before GC"
xoid = test
GC.start
puts "after GC"
}
puts "before call"
f.call
GC.start
puts "after call"
bash-2.05b$
···
On Sunday 24 October 2004 20:23, Michael Neumann wrote:
On Mon, Oct 25, 2004 at 03:15:21AM +0900, Michael Neumann wrote:
> How can I force that the GC frees all unreferenced objects? Basically
> what I want is:
On Mon, Oct 25, 2004 at 03:38:28AM +0900, Simon Strandgaard wrote:
On Sunday 24 October 2004 20:23, Michael Neumann wrote:
> On Mon, Oct 25, 2004 at 03:15:21AM +0900, Michael Neumann wrote:
> > How can I force that the GC frees all unreferenced objects? Basically
> > what I want is:
[snip]
A finalizer was not invoked at life-time... ah, hard to explain, see
yourself:
def take_snapshot
snap =
ObjectSpace.define_finalizer(snap, proc {
p "finalizer called"
})
return snap
end
# BE CAREFUL! Might freeze your computer!
loop do
p "snap"
take_snapshot
end
Memory consumption will grow towards infinity. Why? Probably because the
proc, as it's a closure, implicitly references the snap object, and as
such, snap will never be recycled.
Whereas the following works fine:
$fin = proc { p "finalizer called" }
def take_snapshot
snap =
ObjectSpace.define_finalizer(snap, $fin)
return snap
end
loop do
p "snap"
take_snapshot
end
Regards,
Michael
···
On Mon, Oct 25, 2004 at 10:39:47AM +0900, Bill Atkins wrote: