finalizing(?) instance variables

M

Mr_Tibs

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
 
R

Robert Klemme

------=_Part_2082_16191446.1191928401674
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

2007/10/9 said:
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.
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).

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

------=_Part_2082_16191446.1191928401674
Content-Type: application/x-ruby; name="finalizer.rb"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="finalizer.rb"
X-Attachment-Id: f_f7kb9gqn

IyFydWJ5CgpyZXF1aXJlICdzZXQnCgpjbGFzcyBGb28KICBkZWYgaW5pdGlhbGl6ZQogICAgQHMg
PSAieCIgKiAxMDBfMDAwCiAgZW5kCmVuZAoKbWF4ID0gMAppZHMgPSBTZXQubmV3CgoxMF8wMDAu
dGltZXMgZG8gfGl8CiAgZiA9IEZvby5uZXcKICBpZHMgPDwgZi5vYmplY3RfaWQKICBPYmplY3RT
cGFjZS5kZWZpbmVfZmluYWxpemVyKGYsIGxhbWJkYSB7fG9pZHwgaWRzLmRlbGV0ZSBvaWR9KQog
ICMgdGhpcyBpcyBpbXBvcnRhbnQgdG8gcmVsZWFzZSB0aGUgcmVmZXJlbmNlOgogIGYgPSBuaWwK
ICBwcmludGYgInN0ZXAgJTEwZCAgc2l6ZSAlNWRcbiIsIGksIGlkcy5zaXplCiAgbWF4ID0gaWRz
LnNpemUgaWYgaWRzLnNpemUgPiBtYXgKZW5kCgpwcmludGYgIm1heCAlNWRcbiIsIG1heAo=
------=_Part_2082_16191446.1191928401674--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top