FREEING MEMORY IN RUBY

Posts

Pages: 1
Hi everyone,

I was wondering about best practices for freeing up memory in Ruby programs. For example,


@memory_hog = []
memory_piglet1 = Piglet.new(gigantic_number) #Create new piglets.
memory_piglet2 = Piglet.new(gigantic_number)
memory_piglet3 = Piglet.new(gigantic_number)


@memory_hog.push memory_piglet1, memory_piglet2, memory_piglet3
#Add piglets to memory hog array.

create_crap = true
@memory_hog.each do |i|
i.feed(create_crap)
#Call the function "feed" for each piglet, which creates the new object
#"crap" out of a gigantic_number.
end

@memory_hog = [] #Get rid of all piglets?

In running this code, is there any crap left over?
There's still a reference to each piglet in the memory_piglet# variables so the garbage collector won't dispose of those instances.
Hmm, I'm interested; what purpose does this serve to the layman? It sounds pretty cool.
Trihan
"It's more like a big ball of wibbly wobbly...timey wimey...stuff."
3359
You don't have a yield, so feed won't do anything.
author=Feldschlacht IV
Hmm, I'm interested; what purpose does this serve to the layman? It sounds pretty cool.

It's just making sure that I don't do something like make a dialogue box, and then, it never gets disposed of properly.

Even though it's possible to make sprites invisible, that Sprite object will still exist until you set it to something else. So, if I end up creating a dialogue box, and it never gets properly eradicated, and then, I make another later on, and another, and another, all of that is using up memory. Soon, I just run out of memory. That's the term "memory leak".

In Ruby, you have to release things by turning them into other things, or nothing, with "nil". ("@memory_hog = nil") But for me, the difference in efficacy between "@memory_hog = nil" and something like "@memory_hog = [ ]" (which says "this is a new array") isn't clear.

For example, in the past, I've noticed that the garbage collector is kind of slow. If you do "@orphaned_object = Sprite.new" and then "@orphaned_object = nil", it will take about a second or two before @orphaned_object truly disappears. If @orphaned_object were actually a complicated parent object which had made its own child objects, and you want to get rid of all of those child objects at once by setting that parent object to nil, do you have to wait for the garbage collector before turning @orphaned_object into something else again?

author=GreatRedSpirit
There's still a reference to each piglet in the memory_piglet# variables so the garbage collector won't dispose of those instances.

But since they're not instance variables, with the "@", doesn't that mean that they'll be garbage-collected as soon as that block of code finishes executing?

(Oops, that reminds me, memory_hog should be @memory_hog to show that it's persistent.)

@crap becomes an object of each of the piglets that got cloned into @memory_hog with "push", so when the piglets in memory_hog get obliterated with...


@memory_hog = []

...does that mean that their "@crap" gets obliterated too? I'm wondering just how many things I need to set to nil.
Once the piglet variables move out of scope then the reference ends, barring any weird structure code like yield. I thought feed was just a normal method but I don't know some of the weird shit ruby can do nor how it would affect the garbage collector.

(I also assumes each new instance of the piglet doesn't have unmanaged memory in it and needs a call to it so the piglet can clean it up)
Pages: 1