views:

1631

answers:

3

When writing fairly typical Mac code in an OS X 10.5+ environment, what are the disadvantages to using garbage collection?

So far everything else I've written has been either 10.4 compatible or on the iPhone, so I've become fairly comfortable with retain/release, but now that I'm working on a larger project that's 10.5 only I'm wondering if there are any downsides to just going ahead and using the Objective-C 2.0 garbage collector.

What do you guys think?

+2  A: 

If there is the possibility to port your application to the iPhone, you shouldn't use it.

Garbage collection might have adverse effect on performance if you have special use cases. Without GC, you have precise control on destruction of the object, which is not the case GC world. In most projects, it's worth it to turn on GC as it's less error prone and easier.

In theory, memory management without GC can always be faster that with GC, however, it's not the case in most practical applications (since GC is usually more optimized that human being).

Mehrdad Afshari
In theory GC could be faster! It's the case when system doesn't need to free memory before your program quits - then you get faster allocation (just next chunk off the heap) and never spend time on releasing memory.
porneL
Yeah, sure. But in theory, you can always write a GC that manages your memory in a non-GC environment. ;)
Mehrdad Afshari
no, a GC cannot reclaim memory like a non-GC system. It never knows when to reclaim until it does the collection. Its possible to have a mix of both, allocating from the heap and freeing when it goes out of scope (similar to using() constructs), but not using "pure" GC.
gbjbaanb
Your comment is simply untrue. Automatic collection performs better than manual collection in many cases. (Because collection is grouped into one operation, rather than done ad-hoc whenever an object is no longer in use.)
jrockway
+3  A: 

I prefer handling memory management myself, simply because I like to have that level of control. I know from experience in other languages (C#) that GC doesn't allow you to completely ignore memory issues, and that's the same in Cocoa with things like weak references and callbacks using (void *) where the object isn't explicitly being owned by another object. You're basically trading one set of challenges (memory leaks) for another. Personally I don't tend to make too many memory management mistakes these days, and the ones I do make are pretty easy to track down.

There are some situations (such as implementing data source methods for an NSOutlineView, where you don't want to retain the object being provided to the outline view) where I've thought GC would be really helpful, but I haven't done any real tests with it yet.

Apple lists some other advantages and disadvantages in the GC programming guide.

Marc Charbonneau
Manual memory management is a premature optimization.
jrockway
He's not doing it to optimize.
Mk12
Don't you mean to say, "ignore memory issues, and that's NOT the same in Cocoa"?
Yar
+10  A: 

If you are writing new Cocoa code and targeting Mac OS X 10.5, use Objective-C garbage collection.

If you are writing some code that may also need to run on the iPhone, you can write and test that code for both models very easily by keeping that code in a separate framework, writing it with property -retain and -release use, and setting both your framework and your unit test target for it to GC-supported rather than GC-only.

Xcode will run your unit test bundle twice, once with GC on and once with GC off, and your framework will be usable under both execution models. Then if you eventually want to bring that model-level code to the iPhone, you can put it in an iPhone-targeted static library or include it directly in your iPhone project.

Regardless of whether you're considering running your code on the iPhone, though, you should definitely target garbage collection if your application will require Leopard. It will ease development and the Objective-C garbage collector performs quite well.

Chris Hanson