On Sun, 22 Jan 2006, Ilmari Heikkinen wrote:

> The result? Float allocations fell to 700 per frame from the original
> 3000. And now I'm getting a GC run "only" every 36 frames. Not perfect
> by any means, but a decent start.

Good Stuff.

> Have stories of your own? Tips for memory management? Ways to track
> allocations? Post them, please.

Ok, here goes...

Useful trick one...

Just because Ruby has GC, doesn't mean it knows what is Garbage, merely 
what isn't currently referenced.

ie. Remember to actively drop references you will never need via things 
like...

   $big_hairy_global = nil
     or
   @fat_instance_variable = nil

Trick Two...

Memoization

class Foo
   def initialize( thing)
   end
end

foo = Foo.new( thing)

becomes...

class Foo
   @@memo = Hash.new{|hash,key| hash[key] = Foo.new( key)}

   def create_foo( thing)
    @@memo[thing]
   end

   def initialize( thing)
   end

end


foo = Foo.create_foo( thing)


Trick 3

Sometimes it doesn't pay to iterate over each line.

Sometimes just read the whole ruddy file in in one bang shoot and regex 
over the whole thing.

ie. Instead of...
open( 'foo.txt') do |inf|
   inf.each_line do |line|
     line.chomp!
     if line =~ /thingy/
      # do stuff
     end
   end
end

Try...

IO.read( 'foo.txt').scan(/thingy/){ #do stuff}

John Carter                             Phone : (64)(3) 358 6639
Tait Electronics                        Fax   : (64)(3) 359 4632
PO Box 1645 Christchurch                Email : john.carter / tait.co.nz
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.