Paul Brannan <pbrannan / atdesk.com> wrote in message > 
> 1) Please do not do this, especially in library code.  If you redefine
>    Array#clone, you will break someone's code.  It's preferable to pick
>    another name for your method, and if possible, move it outside of
>    Array (since this method can be used with more than just arrays).

Yup. I've renamed it to some other name...

> 
> 2) I suspect that the reason you are consuming memory is somewhere else
>    and is not in this method, since the method does nothing but copy the
>    array.  What is the size of the array that you are cloning, and how
>    many arrays are you cloning?  What other information can you give us
>    that might be helpful?
> 

I've isolated the problem, and it's something to do with #clone and
how the GC interacts with cloned objects. Apparently cloned objects
are not garbage collected, I've got no idea why not..

If I do this:

class Foo
  def my_clone
    return self.clone
  end
end

Objects doesn't get GC'ed (because finalizers are not fired up) and
memory will be consumed at 2MB/s until system swaps...

But if I do this:

class Foo
  def my_clone
    return Foo.new
  end
end

The cloned object get garbage collected (I know via appropriate
finalizers).

So the million dollar question is: Why the GC is not able to
mark/sweep cloned objects, whereas it can obviously GC fresh
instances?