# Memory implications for persistent Proc objects

**URL:** <https://www.ruby-forum.com/t/memory-implications-for-persistent-proc-objects/152886>\
**Category:** Ruby\
**Created:** [November 8, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/memory-implications-for-persistent-proc-objects/152886 "2008-11-08T23:14:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [November 8, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/memory-implications-for-persistent-proc-objects/152886/1 "2008-11-08T23:14:15Z")

</div>

What are the memory implications for keeping a Proc/closure around  
especially those that are defined in a global scope? Is Ruby clever  
enough not to keep objects in the Proc closure that are never  
referenced in that closure? I’m worried where I have the following  
scenario

big\_object = BigObjectNeedsLotsOfRAM.new  
my\_proc = Proc.new { … } # big\_object not referenced in my\_proc  
big\_object = nil  
my\_proc.call

Will the memory allocated to big\_object remain in use because it was  
alive when my\_proc was created despite it not being referenced or used  
in my\_proc?

Farrel

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [November 9, 2008, 9:11am UTC](https://www.ruby-forum.com/t/memory-implications-for-persistent-proc-objects/152886/2 "2008-11-09T09:11:45Z")

</div>

Farrel L. wrote:

> big\_object = BigObjectNeedsLotsOfRAM.new  
> my\_proc = Proc.new { … } # big\_object not referenced in my\_proc  
> big\_object = nil  
> my\_proc.call
> 
> Will the memory allocated to big\_object remain in use because it was  
> alive when my\_proc was created despite it not being referenced or used  
> in my\_proc?

Clearly not, because big\_object has been assigned to nil. That is:  
big\_object may exist in the environment of my\_proc, but it’s the _same_  
big\_object variable that you’ve assigned outside too.

Now, if you had not assigned to big\_object, but just let it drop out of  
scope, then I wouldn’t be so sure. However you said explicitly that  
you’re only interested in what happens at the top level, and you can’t  
make things drop out of scope at that level.
