On Fri, Mar 30, 2007 at 04:10:04PM +0900, Robert Klemme wrote: > On 28.03.2007 23:41, Pratik wrote: > >I've already called GC.start and yet some objects are not being freed. > >However, I'm still uncertain about memory leaks in ruby code and > >looking for an example of any such case. > > A memory leak comes into existence when some piece of code holds longer > onto an instance than necessary / intended. Look at this: > > class Foo > @instances = [] > > def self.register(obj) > @instances << obj > end > > def initialize > Foo.register(self) > end > end > > Instances register with their class but never deregister - so they can > never be collected and use up memory. My guess would be that you'll > have a hard time writing a program that is able to track these without > actually running the code. IMO, that's not a memory leak, that's correct behaviour. You created some objects and purposely held on to references to them. At any point later in the code someone might do Foo.class_eval { @instances.[n] } My definition of a memory leak would be when memory has been allocated but there are *no* remaining references to it available anywhere, meaning that it has been effectively lost and can never be freed. This shouldn't/can't happen in pure Ruby code. But it can certainly happen in C extensions, where memory has been malloc()d but the pointer forgotten about. Regards, Brian.