On Mon, Nov 9, 2009 at 5:46 PM, Ralph Shnelvar <ralphs / dos32.com> wrote:
> Florian,
>
> If there is no use count, how does Ruby know that there are no
> references to the object that should be freed?

As Florian said, MRI ruby uses a mark-sweep GC algorithm,  when it
needs to GC it traces out references from a set of root objects, and
marks any objects which are reachable recursively.

Then it frees any objects which aren't marked.

Reference counting might seem simpler, but it is expensive because the
count needs to be maintained everytime a reference changes, and it
can't reclaim cyclic garbage which can't be reached from a root:


       a -> b -> c
       ^            |
       *------------+

Mark/Sweep is a fairly primitive GC technique, it was probably the
second technique applied in the long history of GC.  There are more
recent techniques such as generation scavenging which makes use of the
observance that most objects in a uniformly object-oriented language
like Ruby either live a very short, or a reasonably long life time
with the preponderance being short-lived. Generation scavenging
collects the short-lived objects very efficiently, and typically uses
mark-sweep less frequently to clean up the older ones.

-- 
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale