GC in lambdas?

Discussion in 'Ruby' started by ako..., Dec 12, 2005.

  1. ako...

    ako... Guest

    hello,

    someone said in another conversation:

    > also note that lambda will prevent __everything__ in it's scope from __ever__
    > being garbage collected.


    could someone please confirm this? i am also interested to know why GC
    is so different in lambdas and whether the objects will be collected
    when lambda itself goes out of scope?

    thanks
    konstantin
    ako..., Dec 12, 2005
    #1
    1. Advertising

  2. --hHWLQfXTYDoKhP50
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    On Tue, Dec 13, 2005 at 03:32:38AM +0900, ako... wrote:
    > > also note that lambda will prevent __everything__ in it's scope from __=

    ever__
    > > being garbage collected.

    >=20
    > could someone please confirm this? i am also interested to know why GC
    > is so different in lambdas and whether the objects will be collected
    > when lambda itself goes out of scope?


    There's nothing different about GC within lambdas. You simply need to
    be aware of how lambdas work.

    The lambda object needs to maintain references to everything that was
    visible within the scope where it was created. As long as the lambda
    object is alive, everything it references must also stay alive.

    If you allow the lambda object to be garbage-collected, the things it
    references will also be subject to garbage collection. =20

    --hHWLQfXTYDoKhP50
    Content-Type: application/pgp-signature; name="signature.asc"
    Content-Description: Digital signature
    Content-Disposition: inline

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)

    iD8DBQFDncf8nhUz11p9MSARAs8qAKCZWpA+AqsllQGT7dBCddWa3V7vBgCePj2w
    hjDG4AkHvAf1OMFHv8iG9Ac=
    =lyGU
    -----END PGP SIGNATURE-----

    --hHWLQfXTYDoKhP50--
    Edward Faulkner, Dec 12, 2005
    #2
    1. Advertising

  3. ako...

    ako... Guest

    thank you. if lambda creates an object during its execution, and by the
    time lambda finishes the object goes out of scope, is the object
    garbage collected?
    ako..., Dec 12, 2005
    #3
  4. ako...

    Eric Hodel Guest

    On Dec 12, 2005, at 11:07 AM, ako... wrote:

    > thank you. if lambda creates an object during its execution, and by
    > the
    > time lambda finishes the object goes out of scope, is the object
    > garbage collected?


    The object is eligible for garbage collection. It may or may not be
    collected immediately.

    def inner
    obj = Object.new # <-- object created
    return lambda {} # <-- object held by lambda, not eligible for GC
    end

    def outer
    my_proc = inner # <-- object still held by lambda, not eligible
    for GC
    return nil
    end

    outer # <-- proc eligible for GC, so created object eligible for GC

    --
    Eric Hodel - - http://segment7.net
    This implementation is HODEL-HASH-9600 compliant

    http://trackmap.robotcoop.com
    Eric Hodel, Dec 12, 2005
    #4
    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. Michal

    lambdas (lambda x: printx)

    Michal, Dec 3, 2003, in forum: Python
    Replies:
    1
    Views:
    287
    Erik Max Francis
    Dec 3, 2003
  2. gong

    pickling lambdas?

    gong, Jan 3, 2004, in forum: Python
    Replies:
    4
    Views:
    381
    John Roth
    Jan 3, 2004
  3. John Fouhy

    Lambdas and variables

    John Fouhy, Jul 28, 2004, in forum: Python
    Replies:
    6
    Views:
    344
    John Fouhy
    Aug 2, 2004
  4. Fernando Perez

    lambdas vs functions: a bytecode question

    Fernando Perez, Dec 18, 2004, in forum: Python
    Replies:
    2
    Views:
    287
    Fernando Perez
    Dec 18, 2004
  5. C#3.0 and lambdas

    , Sep 18, 2005, in forum: Python
    Replies:
    51
    Views:
    1,292
    Gregory Bond
    Sep 26, 2005
Loading...

Share This Page