Forcing a GC run

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

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

···

On Mon, Oct 25, 2004 at 03:15:21AM +0900, Michael Neumann 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.

   Tobias

[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$

···

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:

--
Simon Strandgaard

Thanks. My problems are gone now.

Regards,

  Michael

···

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. It's exactly what I was looking for.

Regards,

  Michael

···

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?

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

···

On Mon, 25 Oct 2004 09:21:37 +0900, Michael Neumann <mneumann@ntecs.de> wrote:

Thanks. My problems are gone now.

Regards,

  Michael

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:

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