Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Legacy Documentclose button

Important: The Java API for Cocoa is deprecated in Mac OS X version 10.4 and later. You should use the Objective-C API instead. For a tutorial on using Cocoa with Objective-C, see Cocoa Application Tutorial.

Learn Memory Management in Cocoa

Garbage collection in Java frees programmers from having to release the memory used by objects that are not referenced by other parts of a running application. However, in general, programmers have little control over the garbage collector. Although the garbage collector attempts to perform its duties during an application’s idle time, sometimes its activities may adversely impact an application’s performance. Certain Java virtual machines, such as HotSpot, contain very efficient garbage collectors and may allow programmers or system administrators to configure them for specific usage patterns. But this approach might negate to some level the advantages of worry-free object instantiation.

The Objective-C language doesn’t have a garbage collector. But Cocoa uses a mechanism based on reference counting to determine when it’s safe to free the memory occupied by an object no longer in use. This mechanism, however, requires programmer intervention. When you create an object or receive an object from a routine that creates it but otherwise doesn’t “own” it and you want the object to persist for an arbitrary amount of time, you tell Cocoa that you want to hold on to (or retain) the object. When you’re done with the object, you tell Cocoa that you want to let go of (or release) the object. Through these messages, Cocoa increments or decrements a counter that indicates how many references to the object exist. When the reference count reaches zero, Cocoa disposes of the object and releases the memory it occupies. The advantage of this approach over Java’s garbage collection is that programmers have more control over when objects are disposed of.

For details on memory management in Cocoa, see Memory Management Programming Guide for Cocoa.



< Previous PageNext Page > Hide TOC


Last updated: 2006-10-03




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice