Forum: Ruby GC in lambdas?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
ako... (Guest)
on 2005-12-12 20:34
(Received via mailing list)
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
Edward F. (Guest)
on 2005-12-12 21:01
(Received via mailing list)
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.
ako... (Guest)
on 2005-12-12 21:10
(Received via mailing list)
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?
Eric H. (Guest)
on 2005-12-12 21:16
(Received via mailing list)
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. - removed_email_address@domain.invalid - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
This topic is locked and can not be replied to.