GC in lambdas?

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

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.

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.

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?

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 H. - [email protected] - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com