From: Charles Oliver Nutter Date: 2010-08-21T09:16:00+09:00 Subject: Re: Ruby GC question (MRI, JRuby, etc) On Tue, Aug 17, 2010 at 12:19 PM, Chuck Remes wrote: > My basic understanding of the garbage collectors in use by the various Ruby runtimes is that they all search for objects from a "root" memory object. If an object cannot be reached from this root, then it is collected. > > Here's a snippet of ruby code. I'm not sure how the GC will treat it. ... > What happens to the instances of Foo created in the call to #run? Since I am not saving them  somewhere (e.g. to an array), do they get collected right away? Not right away on any impl; they're allocated on the heap, so even though they're immediately abandoned they still require GC to run. > If the Foo instances get collected, is it safe to assume the Baz and Quxxo instances are being collected at the same time? Does their existence prevent the Foo instance from being collected? They would not; no external references to the Foo instance ever exist on the heap or on the stack. As far as how this behaves in JRuby: since the object is short-lived, it would never make it out of the "eden" space on the heap, and with the JVM's GC that means it would basically have no GC cost (young objects that don't survive even a single GC cycle are practically free). The only cost you'd be paying would be the allocation and .new costs. - Charlie