Objective-C 2.0: The Garbage Collector

Before Java was conceived (let alone used), before C++ became the de facto application development language — certainly before it got STL and compilers worthy of mention — NeXT Inc. licensed Objective-C, a Smalltalk resembling, object oriented true C-superset, as the main language for its NeXTSTEP operating system. Objective-C and the (typically) accompanying APIs built upon it by NeXT and whose successors now reside in OS X as ‘Cocoa’, were many years ahead of their time. When I first coded in Objective-C, in 2002, more than 15 years since the introduction of the language, I was amazed at its simple and elegant structure and immensely powerful features; coupled with the Apple APIs, it represented by far the best development platform I’d used: powerful, yet simple, elegant and fast with a good balance between dynamic, runtime-dependent and performance optimised, low-level features. Objective-C was quite popular in some corporate environments in the early 1990s, due to the ease of use of developing GUI applications under the NeXT- (and later Open-)STEP frameworks. It was also, as is famously and often quoted in press, used by Tim Berners-Lee to write the first Web browser.


Times change, however: .Net 2.x (and now 3.x) are much further ahead in some areas and are being constantly worked upon by Microsoft. So is ye olde Java, that — after years of stagnation — has picked up in pace and in the years between 2004 and today has been enriched with a number of much-needed features such as Generics, {un}boxing etc. along with countless improvements of its performance, the standard massive library, the compiler etc. While there had been indications of Objective-C’s renewed development for years, it was only at Apple’s WWDC, in August 2006, that the existence of the next revision of Objective-C, named ‘version 2.0′ was confirmed by Apple. To many developers, myself included, this, along with the port of Sun Microsystems’ excellent DTrace framework (at the core of a new debugging tool called XRay), a new version of XCode and an assortment of new APIs and developer tools, represented a consolation, for Apple’s presentation of Leopard seemed very uninteresting at the time.
One of the Objective-C 2.0 major features that caused a lot of noise in online forums and news sites was the introduction of a Garbage Collector. Contrary to languages such as C++ and Java, Objective-C provides a very interesting, split-in-the-middle, easy to use and at the same time powerful construct: the autorelease pool. An autorelease pool is effectively a reference-counted memory management mechanism that simplifies the handling of objects, pointers to those objects and their owners. It is not completely automated: a developer still has to worry about the object graph, the references and the overhead of managing them (albeit it being extremely minor compared to — say — C++). In Objective-C 2.0 pointers are qualified as __weak or __strong. The former causes the runtime to generate garbage collection events whereas the latter suppresses the generation of such events (the default is __weak). The introduction of Garbage Collection in Objective-C was met with some enthousiasm, among developers that enjoyed programming in this awesome language, but were concerned about its slow development and its position among the state of the art of the mid to late 2000s. Others, people that felt comfortable with their pointers and autorelease pools, found it excessive, a useless addition to an already excellent and easy to use language that would only benefit less-than-capable programmers at the expense of performance.
There are numerous other additions to the language: ‘foreach’ type iterators that facilitate iteration of elements in a collection, properties, somewhat similar to publicly accessible instance variables, but with smarter/easier syntax, compiler metadata (attributes) as is the case with gcc and C (especially coding hints) and a revamped structure for protocols with the introduction of @optional, @required and concrete protocols.
While I welcome most of the additions, I am also somewhat divided about the introduction of the GC in Objective-C 2.0. Having said that, I probably lean towards the first group: I am very happy that Apple is actively (within its powers) trying to modernise Objective-C and since the Garbage Collector is only optionally enabled, I do not consider it a bad addition: I am certain that for a considerable number of applications it will enhance developer productivity and further minimise the time-to-market. Now, if only Apple open sourced the Objective-C runtime too…