On Fri, 2002-08-23 at 12:18, William Djaja Tjokroaminata wrote:
> Hi,
> 
> As I am developing my code in Ruby/C, I cannot help to ask the question,
> why does Ruby use the mark-and-sweep gc, instead of reference counting
> such as that used by Python?

A) Reference counting breaks.
B) Even Python doesn't use ref counting any more
C) Ref counting can be even slower in circumstances, depending on memory
allocation
D) Ref counting wouldn't work in even a single large Ruby project I've
done, without me having to do metric shitloads of extra work (i.e.,
clearing references and such, to avoid ref loops)

> 
> For the majority of Ruby codes, probably the kind of gc does not matter
> (just as when I first coded in Ruby), but now when I started to code in C,
> it seems that the mark-and-sweep gc can really get in the way.
> 
> I think one of the problems with reference counting is the circular
> reference, which cannot be handled by the gc.  However, now the problems
> with mark-and-sweep gc for my specific applications are:
> 
> 1) It is linear (O(N)) in the number of Ruby objects, and therefore it
> seems that it will not scale well.  Yes, probably for the majority of Ruby
> applications, the cost of gc invocation is small as compared to all the
> other functions in the Ruby code, i.e., 
> 
>     cost = eps * N
> 
> where eps is a small number.  However, no matter how small eps is, as N
> grows large, eventually the gc cost cannot be neglected.

Admittedly, mark and sweep can suck.  There are much better GC methods
out there.  Dunno, maybe future Ruby will use one (I'm not authoritive
on this at all).

> 
> 2) When the Ruby objects are static, gc invocation is a waste.
> 
> Regarding 2), I think I can solve it by manually disabling and invoking
> gc.  However, I still cannot solve 1).  Therefore,
> 
>     "Is there a way to manually and explicitly delete a Ruby object (from
> C), just like Python's delete function, so that the sweep (and mark) phase
> needs not be done?"

This would break.  Bad.  Lots of internal objects would never get
cleared, either.  There is more to the language than just what you
allocate/free.

> 
> I never had a formal training on the subject of gc, and therefore I don't
> know whether my question above is simply impossible because of the
> fundamental natures of mark-and-sweep gc.

Bah.  You don't need formal training.  Just do some Google searches,
play with the Ruby source, etc.  I'm written several memory manages for
C/C++ projects, and let me tell you - even a poorly implemented GC
system is easier and less stressful than the best of reference counting
systems.  A good GC is pure bliss. ~,^

> 
> Regards,
> 
> Bill