From: Ralph Shnelvar Date: 2009-11-10T09:35:15+09:00 Subject: Re: Good or best way to allocate a large array Rick, Ok ... but the point is that the objects are not gc'd when things go out of scope but, instead, when Ruby needs memory, right? And if you have a huge array with gobs of objects, the gc is gonna take a while? Ralph Monday, November 9, 2009, 4:06:23 PM, you wrote: RD> On Mon, Nov 9, 2009 at 5:46 PM, Ralph Shnelvar wrote: >> Florian, >> >> If there is no use count, how does Ruby know that there are no >> references to the object that should be freed? RD> As Florian said, MRI ruby uses a mark-sweep GC algorithm, when it RD> needs to GC it traces out references from a set of root objects, and RD> marks any objects which are reachable recursively. RD> Then it frees any objects which aren't marked. RD> Reference counting might seem simpler, but it is expensive because the RD> count needs to be maintained everytime a reference changes, and it RD> can't reclaim cyclic garbage which can't be reached from a root: RD> a -> b -> c RD> ^ | RD> *------------+ RD> Mark/Sweep is a fairly primitive GC technique, it was probably the RD> second technique applied in the long history of GC. There are more RD> recent techniques such as generation scavenging which makes use of the RD> observance that most objects in a uniformly object-oriented language RD> like Ruby either live a very short, or a reasonably long life time RD> with the preponderance being short-lived. Generation scavenging RD> collects the short-lived objects very efficiently, and typically uses RD> mark-sweep less frequently to clean up the older ones. -- Best regards, Ralph mailto:ralphs@dos32.com