# Finalizing(?) instance variables

**URL:** <https://www.ruby-forum.com/t/finalizing-instance-variables/116730>\
**Category:** Ruby\
**Created:** [October 9, 2007, 3:58am UTC](https://www.ruby-forum.com/t/finalizing-instance-variables/116730 "2007-10-09T03:58:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [October 9, 2007, 3:58am UTC](https://www.ruby-forum.com/t/finalizing-instance-variables/116730/1 "2007-10-09T03:58:25Z")

</div>

Hi,

I finished writing a fairly large Ruby application and I was careless  
with cleaning up variables. My objects contain references to large  
ActiveRecord objects (instance variables), and I wanted to make sure  
that I don’t specifically need to set these instance variables to nil  
when they’re not used anymore: as long as I set the parent object to  
nil, since there will be no reference to its instance variables, the  
parent object and its instance variable objects should be removed by  
the GC. Is this correct?

For example, I might have this program:

class Blah

attr\_accessor :arr

def initialize  
@arr = Array.new(10000000, ‘TEST’)  
end

end

while (true)  
b = Blah.new  
sleep 5  
end

I noticed that the memory on my machine only goes up at the beginning  
of the program, and then stays constant. I can only conclude that each  
time the loop executes, the old b.arr becomes unreferenced and is  
garbage collected and that the new b.arr is allocated (thus memory  
usage is the same).  
I just need someone to confirm this, since my program freezes after  
running more than 24hours and I can’t find the (memory) leakage.

Thanks,  
Tiberiu

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [October 9, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/finalizing-instance-variables/116730/2 "2007-10-09T13:13:58Z")

</div>

2007/10/9, Mr\_Tibs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi,
> 
> I finished writing a fairly large Ruby application and I was careless  
> with cleaning up variables. My objects contain references to large  
> ActiveRecord objects (instance variables), and I wanted to make sure  
> that I don’t specifically need to set these instance variables to nil  
> when they’re not used anymore: as long as I set the parent object to  
> nil, since there will be no reference to its instance variables, the  
> parent object and its instance variable objects should be removed by  
> the GC. Is this correct?

Correct. Ruby does mark and sweep CG AFAIK.

> end  
> usage is the same).  
> Correct again. You can view the effect with the script attached.

> I just need someone to confirm this, since my program freezes after  
> running more than 24hours and I can’t find the (memory) leakage.

You probably still hold on to more memory than you think.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [October 9, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/finalizing-instance-variables/116730/3 "2007-10-09T20:10:33Z")

</div>

Thanks Robert.
