John Ky wrote:
> When I replace the following code:
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 head.play_back do |root|
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0block.call(root)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 end
>
> With this:
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 head.play_back(&block)
>
> And make other similar changes. =A0The program behaves exactly the same w=
ay,
> but now uses 200MB less.

Consider the following:

def x(&block)
  block
end

def a(&block)
  tmp =3D create_a_two_hundred_mega_byte_object()
  x {block.call} # Create a new block which closes over the current scope
end

def b(&block)
  tmp =3D create_a_two_hundred_mega_byte_object()
  x(&block) # Reuse the block that has been passed to this method.
            # Do not create a new block
end

If I call b {puts "hello"}, the following will happen:
The block {puts "hello"} is created. This block holds a reference to any lo=
cal=20
variable that has been defined in the scope in which I call method b.
Now method b is called with that block as an argument.
Then a 200mb object is created and stored in tmp.
Now x is called. x returns a Proc representing the block passed to b.
The method b ends, tmp goes out of scope and the 200mb object is garbage=20
collected.

If I call a {puts "hello"}, this happens:
The block {puts "hello"} is created. This block holds a reference to any lo=
cal=20
variable that has been defined in the scope in which I call method a.
Now method a is called with that block as an argument.
Then a 200mb object is created and stored in tmp.
Now another block is created, which invokes the block passed to a.
This new block holds a reference to any local variable tmp defined in the=20
method body of a. Specifically it holds a reference to tmp.
Now x is called. x returns a Proc representing this new block.
The method a ends, but the 200mb is still referenced by the Proc that's=20
returned from a. So as long as reference to that Proc is stored, the 200mb=
=20
object will not be garbage collected.

I don't know whether that's what happens in your code, but it's certainly=20
worth looking into.

HTH,
Sebastian
=2D-=20
Jabber: sepp2k / jabber.org
ICQ: 205544826