# GC in lambdas?

**URL:** <https://www.ruby-forum.com/t/gc-in-lambdas/98953>\
**Category:** Ruby\
**Created:** [December 12, 2005, 7:34pm UTC](https://www.ruby-forum.com/t/gc-in-lambdas/98953 "2005-12-12T19:34:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![akoSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@akoSS](https://www.ruby-forum.com/u/akoSS)\
**Post date:** [December 12, 2005, 7:34pm UTC](https://www.ruby-forum.com/t/gc-in-lambdas/98953/1 "2005-12-12T19:34:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![akoSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@akoSS](https://www.ruby-forum.com/u/akoSS)\
**Post date:** [December 12, 2005, 8:01pm UTC](https://www.ruby-forum.com/t/gc-in-lambdas/98953/2 "2005-12-12T20:01:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![akoSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@akoSS](https://www.ruby-forum.com/u/akoSS)\
**Post date:** [December 12, 2005, 8:10pm UTC](https://www.ruby-forum.com/t/gc-in-lambdas/98953/3 "2005-12-12T20:10:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![akoSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@akoSS](https://www.ruby-forum.com/u/akoSS)\
**Post date:** [December 12, 2005, 8:16pm UTC](https://www.ruby-forum.com/t/gc-in-lambdas/98953/4 "2005-12-12T20:16:25Z")

</div>

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](http://segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
