News for hackers. Stuff that matters.
Re:Why use garbage collection by zandorg on Thursday January 22, 2009 @11:41PM (#445945)
I wrote a C++ class (compatible with STL's vector class), and it had an internal reference counter. If the counter went to 0, it would de-allocate the memory and shut the vector down. It works pretty well, and avoids the need for a garbage collector.

It uses constructors and destructors to achieve this.

Re:Why use garbage collection by s3graham on Friday January 23, 2009 @01:00AM (#446044)
That "works" (minus circular references) in simple cases, but it still has bad worst case performance (if your decrements chain, causing many decrements and frees).

And in something like boost::smart_ptr, the cache performance is awful because the increment/decrement is an extra indirection in a separate allocation, which is getting hit constantly.

Re:Why use garbage collection by newt0311 on Friday January 23, 2009 @04:09AM (#446185)
Actually, general malloc performance is horrible and that refcounting implimentation will be the absolute worst. In an accurate GC, the objects can be moved around in memory for more efficient allocation. Add support for generations and it becomes significantly faster than manual allocation. However, refcounting misses all of that and screws up on circular referrences to boot.
Re:Why use garbage collection by s3graham on Friday January 23, 2009 @11:40AM (#446567)
"Actually," normally starts a disagreement, but AFAICT, you restated what I said and agree. Or do I misunderstand?
Re:Why use garbage collection by newt0311 on Friday January 23, 2009 @09:55PM (#447655)
Sorry, you are right. It's my bad grammer getting to me again.
Re:Why use garbage collection by zandorg on Friday January 23, 2009 @06:14PM (#447349)
The purpose of the reference count is to only deallocate memory once. You create it (copy constructor, count = 1), you pass it to a function (count = 2), come out of the function (destructor, count = 1) and finally return from the original function (count = 0, deallocate). It's like passing a hashtable around in Lisp - it doesn't pass a copy, it passes a pointer, and thus is more efficient on memory usage and speed.
Re:Why use garbage collection by brl on Thursday January 22, 2009 @11:47PM (#445953)
What about circular references?
Re:Why use garbage collection by zandorg on Friday January 23, 2009 @12:58AM (#446039)
It's a bit hard to explain, but it's a copy constructor. Not sure of all the details right now, but I think when it is passes to another function (as a parameter), the reference count goes up, and when it returns (destructor), it goes down. I don't think it can be circular, as it uses types, not pointers. The basic idea is the data stays in the same place in memory.