Forcing a GC run

Discussion in 'Ruby' started by Michael Neumann, Oct 24, 2004.

  1. 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
    Michael Neumann, Oct 24, 2004
    #1
    1. Advertising

  2. 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
    Michael Neumann, Oct 24, 2004
    #2
    1. Advertising

  3. 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
    Simon Strandgaard, Oct 24, 2004
    #3
  4. 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
    Michael Neumann, Oct 24, 2004
    #4
  5. 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
    Tobias Peters, Oct 24, 2004
    #5
  6. 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
    Michael Neumann, Oct 25, 2004
    #6
  7. Michael Neumann

    Bill Atkins Guest

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


    On Mon, 25 Oct 2004 09:21:37 +0900, Michael Neumann <> wrote:
    > Thanks. My problems are gone now.
    >
    > Regards,
    >
    > Michael
    >
    >
    Bill Atkins, Oct 25, 2004
    #7
  8. 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
    Michael Neumann, Oct 25, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mdb
    Replies:
    3
    Views:
    4,132
    =?Utf-8?B?TmVpbA==?=
    Sep 20, 2004
  2. DonQ
    Replies:
    2
    Views:
    885
    Amontec Team, Laurent Gauch
    Feb 28, 2004
  3. Jluis
    Replies:
    7
    Views:
    496
    Just an Illusion
    Jun 11, 2004
  4. Earl Teigrob
    Replies:
    2
    Views:
    683
    Earl Teigrob
    Dec 23, 2003
  5. =?Utf-8?B?Y2lzY28=?=

    forcing code to run - httpmodule etc.

    =?Utf-8?B?Y2lzY28=?=, Jun 17, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    735
    Walter Wang [MSFT]
    Jun 20, 2006
Loading...

Share This Page