Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use Objective-C garbage collection when writing for 10.5+?

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?

like image 338
Lawrence Johnston Avatar asked Dec 10 '08 17:12

Lawrence Johnston


People also ask

When should you not use garbage collection?

One fundamental problem with garbage collection, though, is that it is difficult to estimate and manage the actual size of the working set in memory, because garbage collector can free your memory only delayedly. So, yes, when memory is restricted, garbage collection might not be a good choice.

Does Objective C use garbage collector?

Objective-C 2.0 adds garbage collection to the language as a replacement to the traditional retain / release reference counting scheme. Garbage collection is automatic object memory management.

Does garbage collection affect performance?

The most common performance problem associated with Java™ relates to the garbage collection mechanism. If the size of the Java heap is too large, the heap must reside outside main memory. This causes increased paging activity, which affects Java performance.

Is garbage collection Necessary?

It is not strictly necessary. Given enough time and effort you can always translate a program that depends on garbage collection to one that doesn't.


1 Answers

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.

like image 122
Chris Hanson Avatar answered Sep 22 '22 10:09

Chris Hanson