From: Kirk Haines Date: 2010-08-18T02:42:09+09:00 Subject: Re: Ruby GC question (MRI, JRuby, etc) On Tue, Aug 17, 2010 at 11:19 AM, 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. It depends on the Ruby. JRuby and Rubinius have different garbage collectors than MRI Ruby. > 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? Nothing is ever collected right away in the MRI rubies currently. The object will exist in memory until a GC cycle runs. Unless a GC cycle is started manually (GC.start), GC cycles only run when Ruby runs short on preallocated memory. Take a look at this http://www.engineyard.com/blog/2010/mri-memory-allocation-a-primer-for-developers/ or google on the subject and you'll find a number of articles that will explain how it works in more detail than you will get in an email. > 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? It depends on why you are assuming it. If you have an implementation that depends on specific garbage collection behaviors or collection in specific chronologies in order to work right, it is probably not safe to assume anything. If you are just trying to understand the memory behavior of your code, and make sure you aren't doing dumb things that can lead to a memory leak, then yes, it is safe to assume that the Baz and Quxxo instances will be collected along with the Foo. Kirk Haines