oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button Programming With Cocoa

Memory Management in Objective-C


Today we are going to learn about a seemingly intimidating programming topic: memory management in Objective-C.

Memory management allows you to make sure that precious memory space isn't taken up by objects that no longer have a purpose in your application, and that objects that are still needed continue to exist. After an object has been created and you finish using it, it will need to be disposed of so that memory isn't taken up needlessly.

The goal of memory management is to keep your application running like a well-oiled machine. When an object becomes detached from an application's control without being "de-allocated," memory is wasted. This waste of memory is called a memory leak, and if you have too many memory leaks in a large application, your application will go from running at a clip to becoming a sluggish mess. If allowed to continue running indefinitely, the memory leaks in your application will take up more and more memory, and with the way virtual memory works in Mac OS X, this means your hard disk will be accessed more frequently as it picks up the memory spill-over. Accessing the disk can slow down your application, so memory management is a serious concern and it must be handled properly.

Different programming languages dispose of objects in different ways. Java, for instance, implements a memory management system known as automatic garbage collection. In this system, the developer does not need to worry about actively de-allocating unused objects, rather a hidden garbage collector checks each object to see whether or not it is being used by other objects. If the garbage collector decides that an object is not being used by other objects, it will de-allocate that object, freeing up the memory it once occupied.

Comment on this articleDo you have any other questions for Mike about memory management using the Objective-C language?
Post your comments

Also in Programming With Cocoa:

Understanding the NSTableView Class

Inside StYNCies, Part 2

Inside StYNCies

Objective-C uses another form of memory management known as "reference counting." In this system, each object is associated with a count of how many other objects are currently using it, but the reference counts of objects in an application must be actively managed by the developer. The idea is that when an object has a reference count of "0", no other objects are using it, and it can safely be de-allocated to free up memory space.

Both of these memory management schemes have advantages and disadvantages. Automatic garbage collection is easy on the developer because it's entirely automatic. However, this convenience comes at the expense of performance and efficiency. Reference counting on the the hand is very efficient and fast because there is no garbage collector working behind the scenes using up resources. The flip side of the coin is that the increased performance and efficiency of reference counting requires more work on the part of the developer. Fortunately, this is not difficult once you understand the rules to the game. The goal of this column is to teach you these rules -- how to control the reference count of an object using the methods of Cocoa -- to ensure that objects you expect to exist do exist, and objects that are no longer needed are properly disposed.

Pages: 1, 2, 3

Next Pagearrow