[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Forcing a GC run

Michael Neumann

10/24/2004 6:15:00 PM

Hi,

How can I force that the GC frees all unreferenced objects? Basically
what I want is:

x = Object.new
xoid = x.object_id
ObjectSpace.define_finalizer(x, proc { puts "recycled" })

x = nil
ObjectSpace.garbage_collect
puts "after GC"
p ObjectSpace._id2ref(xoid)

What I get is:

before GC
after GC
#<Object....>
recycled

Where I'd have expected:

before GC
recycled
after GC
-> exception RangeError

Any hints?

Regards,

Michael


7 Answers

Michael Neumann

10/24/2004 6:24:00 PM

0

On Mon, Oct 25, 2004 at 03:15:21AM +0900, Michael Neumann wrote:
> Hi,
>
> How can I force that the GC frees all unreferenced objects? Basically
> what I want is:
>
> x = Object.new
> xoid = x.object_id
> ObjectSpace.define_finalizer(x, proc { puts "recycled" })
>
> x = nil

this one is missing of course:

puts "before GC"

> ObjectSpace.garbage_collect
> puts "after GC"
> p ObjectSpace._id2ref(xoid)

Regards,

Michael


Simon Strandgaard

10/24/2004 6:38:00 PM

0

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]


I don't know if this helps you?


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$


--
Simon Strandgaard


Michael Neumann

10/24/2004 7:02:00 PM

0

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]
>
>
> I don't know if this helps you?

Thanks. It's exactly what I was looking for.

Regards,

Michael


Tobias Peters

10/24/2004 8:30:00 PM

0

Michael Neumann wrote:
> How can I force that the GC frees all unreferenced objects?

This is impossible, because ruby's GC is conservative.

You can only rely on all objects getting freed before a clean exit of
the interpreter.

Tobias

Michael Neumann

10/25/2004 12:22:00 AM

0

On Mon, Oct 25, 2004 at 05:54:08AM +0900, Tobias Peters wrote:
> Michael Neumann wrote:
> >How can I force that the GC frees all unreferenced objects?
>
> This is impossible, because ruby's GC is conservative.
>
> You can only rely on all objects getting freed before a clean exit of
> the interpreter.

Thanks. My problems are gone now.

Regards,

Michael


Bill Atkins

10/25/2004 1:40:00 AM

0

How exactly did that happen? I'm confused. :)


On Mon, 25 Oct 2004 09:21:37 +0900, Michael Neumann <mneumann@ntecs.de> wrote:
> Thanks. My problems are gone now.
>
> Regards,
>
> Michael
>
>


Michael Neumann

10/25/2004 8:59:00 AM

0

On Mon, Oct 25, 2004 at 10:39:47AM +0900, Bill Atkins wrote:
> How exactly did that happen? I'm confused. :)

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