TweetFollow Us on Twitter

Developer to Developer: Thanks for the Memory-Part 2

Volume Number: 26
Issue Number: 11
Column Tag: Developer to Developer

Developer to Developer: Thanks for the Memory-Part 2

A look at how memory is managed in Objective-C

by Boisy G. Pitre

Introduction

If you followed last month's Developer to Developer column, you'll recall that we went through an introduction to memory management in Objective-C and Cocoa applications. That article was well suited to those who were new to Mac and iOS development, and touched on a number of concepts that are unique to Apple's development environment. For comparison, we also looked at how memory management is done in C and C++. Also, we touched on how Java programmers are greatly insulated from any memory considerations due to garbage collection.

This month we'll build upon the previous article and wade a little deeper into the waters of Objective-C memory management. We will start out by looking at a special subclass of NSObject that we can use to see just how our objects behave when they are created, retained, released and deallocated. We'll also take a look at the NSAutoreleasePool class, which gives us a convenient way to defer the release of an object and the return of its memory to the system. Finally, we will take Instruments, Apple's robust developer tool, for a spin.

Seeing Is believing

As we discussed last month, memory management in Objective-C consists of requesting an ownership interest in an object by retaining it, and relinquishing that ownership interest by releasing it. These operations can occur numerous times within an application for the same object. Balancing them is key to insuring that the memory that an object occupies is not left to leak while the application runs. Conversely, we must guard against the memory being returned to the system too soon, which could result in a crash, depending upon how that object is accessed later.

How convenient would it be to actually visualize this process happening in real-time? Well, it can be done, and quite readily, by subclassing NSObject and creating a new base class which overrides the retain and release methods, among others. And that's exactly what we will do. I highly recommend that you follow along by downloading the code for this month's article from the MacTech FTP source archive at ftp://ftp.mactech.com and launching the project file in Xcode. The application is made up of simple, contrived examples that are useful in illustrating the mechanics of object allocation and deallocation.

When determining what methods to override, we can consult two resources: the NSObject Class Reference (available in the Xcode's Developer Documentation, accessible from the Xcode Help menu) or the NSObject.h header file itself. Let's take the header file route and take a peek at NSObject.h directly. To do this easily from within Xcode, go to the File > Open Quicky... menu option, then type NSObject.h. It should appear in the drop down box; select it and it will be displayed in an Xcode text editor window.


Figure 1 - Opening the NSObject.h header file

The header file is composed of several sections: basic protocols, the base class, discardable content and object allocation/deallocation. For this article, we're going to focus on methods in the basic protocols and base class sections of the header file (starting at lines 11 and 63 respectively of the header file in the 10.6 SDK). Of the methods declared in the basic protocols section, let's override the following:

- (id)retain;
- (oneway void)release;
- (id)autorelease;

Similarly, let's override the following methods declared in the base class section:

- (id)init;
- (void)dealloc;

Lastly, for informational purposes, we'll override this method:

+ (id)allocWithZone:(NSZone *)zone;

There may be some questions as to the choice of methods to override. Remember, we are trying to capture the memory management operations in a way that allows us to visually confirm them as they happen. Most of the above methods are vectors for the retain count changing. By overriding these methods with methods in a subclass that (a) calls the same method in NSObject, and (b) logs the call itself and the retain count, we can see Objective-C memory management in action.

As mentioned earlier, overriding the methods requires subclassing NSObject. We'll create a new class just for the Developer to Developer column, DDObject, as a direct descendant of NSObject, so any class who inherits from DDObject will automatically benefit from the methods that we will embellish. Let's start out by looking at the code for the retain and release methods:

- (id)retain;
{
   id result = [super retain];
   NSLog(@"[%@ retain] (retainCount = %d)", [self className], [self retainCount]);
   return result;
}
- (oneway void)release;
{
   NSLog(@"[%@ release] (retainCount = %d)", [self className], [self retainCount] - 1) withLevel:TBLogLevelInfo];
   [super release];
}

Both methods mimic the return values of their original definitions in NSObject.h and use the NSLog() function to show the name of the class and the retain count. The retain method returns the retain count after the superclass' retain method is called, while the release method shows the retain count first. This ordering insures that we see the true retain count value at the appropriate place and time.

The init and dealloc methods are also points where observing the retain count can be instructive, so we'll extend these as well:

- (id)init;
{
   if (self = [super init])
   {
      NSLog(@"[%@ init] (retainCount = %d)", [self className], [self retainCount]);
   }
   
   return self;
}
- (void)dealloc;
{
   NSLog(@"[%@ dealloc]", [self className]);
   [super dealloc];
}

Even though we explicitly call retainCount in the init method, it will always print a retain count of 1. The dealloc method is called only when the retain count goes to 0; since a release call precedes this, we'll forego logging the retain count, and simply log that we are deallocating the object's memory here.

Finally, we'll override the allocWithZone: method, which will clue us in when an allocation is taking place (as we'll see shortly, the alloc method actually calls allocWithZone: with a zone of 0):

+ (id)allocWithZone:(NSZone *)zone;
{
   NSLog(@"[%@ allocWithZone:%d]", [self className], zone);
   return [super allocWithZone:zone];
}

Now that the pieces are in place, let's take a look at memexplore.m. This file contains the main() function, several test functions, and the interface and implementation for the MemObject class. This class extends the DDObject class which we just reviewed, so we would expect to see some interesting output when we run the test. Let's go ahead and do that now. First, build the memexplore target (Build > Build). Next, ensure that the Debugger Console is in focus so that the logging results can be seen (Run > Console). Finally, run the application (Run > Run). A menu appears where you can type the number of the test to perform. Let's run the allocRunRelease test by typing 1 then the return key.


Figure 2 – Output of the allocRunRelease test

The output of this test clearly shows the steps in which our MemObject comes to life, runs, then is finally released. As expected, the init method shows that the retain count is 1. The run method is then called, and finally the release method. Recall that this method decrements the retain count by 1; this is confirmed by the retain count falling to 0, and the dealloc method being implicitly called after that. Just to be convincing that the retain count can go higher, the allocRunReleaseMultiple test performs a retain after allocating and initializing the MemObject object, as well as an extra release. The net effect is the same: the object's dealloc method is called when the retain count falls to 0. Go ahead and run this test as well.


Figure 3 - Output of the allocRunReleaseMultiple test

One might conclude that the "hands-on" memory management aspect of Objective-C is a bit laborious. After all, we not only explicitly allocate and initialize an object, we must also take care to properly release it. Not releasing an object after we are done with it denies the use of that memory elsewhere; if an application doesn't release an object properly, it will stay around until the application quits, which could be a short time or a long time. Even though current systems contain gigabytes of memory and can accommodate a bit of sloppy memory management, it is considered bad programming practice to tolerate memory leaks. On mobile devices such as the iPhone and iPad where resources are limited, it is even more critical to wipe out these types of memory leaks.

Swimming In The NSAutoreleasePool

As we discussed, balancing retains with releases insures that we avoid memory leaks or crashes. It is a disciplined approach, and forces us to think about the lifetimes of our objects. However, Cocoa gives us a bit of a reprieve from the tedium of retain count management. We can make things a little easier for ourselves by conveniently deferring the return of an object's memory to the system using a special type of class provided by the Cocoa framework: NSAutoreleasePool.

An autorelease pool acts as a sort of dumping site for objects; upon receiving an object, the pool dutifully records a reference to the memory location of objects for later releasing. Any object can be relegated to the autorelease pool by having the autorelease message sent to it:

   SomeObject *s = [[[SomeObject alloc] init] autorelease];

The autorelease message allows us to pass complete responsibility of releasing the object to the NSAutoreleasePool. By doing this, we essentially wash our hands of further worry about the lifetime of the object and the memory that it is taking up. In the above code fragment, the object s receives the autorelease message after the alloc and init messages; subsequent to that, we can use the object as needed but should not send the release message to the object. That will be performed by the autorelease pool at a later time.

Exactly where autorelease pools are created depends upon the context in which you are writing your program. In Cocoa applications, an autorelease pool is created for you automatically, so you don't have to concern yourself with its creation. However, if you are using threads, you will need to create and manage your own autorelease pool for that thread. And in the case of a command line based program such as memexplore, it is necessary to create an autorelease pool as well.

Autorelease pools can be created many times, with the most recent pool being the one that receives any objects that receive the autorelease message. In essence, autorelease pools are stacked as they are created. When the topmost autorelease pool is destroyed, the next autorelease pool will receive autoreleased objects. Destroying an autorelease pool is similar to destroying any object: sending a release message to the pool will cause it to in turn send release messages to all objects that it holds, and finally the pool itself is deallocated. A slight twist on this is that since the release of Objective-C 2.0 and garbage collection, the drain message is the desired message to send to an autorelease pool instead. This message performs the same function as the release message, but does additional work in a garbage collected environment. For our code, we'll use the drain message when releasing our pools.

Can we peek into an autorelease pool? We certainly can, thanks to the NSDebug.h header file's NSAutoreleasePoolDebugging category. The showPool message, when sent to an autorelease pool object, will display the contents of all pools in the pool stack to the standard error path. We use this debugging method in several of our test programs to illustrate what the pool looks like just before it is drained. To illustrate this, run test 1 (allocRunRelease) then test 5 (allocRunReleaseWithPoolOverRetain). The final output will look like this:

==== top of stack ================
  0x100110b00 (NSCFString)
  0x100110ae0 (NSCFString)
  0x100110920 (MemObject)
  0x100110a70 (NSCFString)
  0x100110a50 (NSCFString)
  0x100110a30 (NSCFString)
==== top of pool, 6 objects ================
  0x1001109b0 (NSCFString)
  0x1001109e0 (NSCFString)
  0x100110990 (NSCFString)
  0x1001108b0 (NSCFString)
  0x100110070 (NSCFString)
==== top of pool, 5 objects ================

The pool appearing on the very top of the stack was created in the allocRunReleaseWithPoolOverRetain() function and has 6 objects, including a MemObject which we overretained and is just leaking. The next pool was created in the main() function has 5 objects. You may be wondering what is going on with all of the NSCFString objects in the autorelease pool. Those are the various string literals which appear in the NSLog() functions that have been executed during the program's run. As objects themselves, these strings require memory management, and they are automatically added to the autorelease pool when initialized.

Pool Hazards

As convenient as autorelease pools are, they must be used with care. The same problems that we discussed last month (overretaining/underreleasing or overreleasing/

underretaining) can still lead to memory leaks or crashes. A common mistake that many beginning programmers make is to send a release message to an object after it has been sent an autorelease message. The net effect of that transgression is that the object will end up being overreleased, and a crash is likely. The allocRunReleaseWithPoolOverRelease test illustrates this poignantly.

   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   NSLog(@"- allocRunReleaseWithPoolOverRelease -");
   MemObject *s1 = [[[MemObject alloc] init] autorelease];
   [s1 run];
   [s1 release];
   NSLog(@"----------------");
   [NSAutoreleasePool showPools];
   [pool drain];

Note that the autorelease message is sent to the s1 object upon creation, then a release message follows. It is at that point which the object's retain count goes to 0 and its dealloc method is called. Since its reference is also in the autorelease pool, the simple act of sending the showPools method is enough to trigger an access exception and crash the program.

Another interesting hazard is having no autorelease pool at all. Without an autorelease pool, objects sent an autorelease message (including NSCFString string literals that we saw above) have nowhere to go, and just leak all over the place. If you ever see a message like this coming from your application:

*** __NSAutoreleaseNoPool(): Object 0x100110770 of class NSCFString autoreleased with no pool in place - just leaking

then you know that somewhere, an autorelease pool is needed but doesn't exist. This most commonly happens when using threads and failing to create an autorelease pool. As a rule, your thread's entry point method should create an autorelease pool at the beginning, then drain the pool at the end.

Inspecting Memory Leaks With Instruments

Apple's Instruments is part of the Xcode development suite, and is a powerful tool for strengthening and bulletproofing your applications in a number of areas. When it comes to memory usage and management, it is particularly useful, and has handy two templates that are a must: Allocations and Leaks. The former template shows you exactly what objects and how many are being allocated by your application. The latter gives you insight into where your application may be leaking memory.

Typically, Instruments can be invoked from within Xcode's Run menu. Given that our application is a windowless application whose input and output appear on the console, we'll start Instruments from the Finder and then attach to the running process.

Before starting Instruments, go ahead and run the memexplore program from within Xcode. Now let's start Instruments by navigating to the /Developer/Applications folder in the Finder and double-click the Instruments icon. You will see a window with a drop-down sheet asking for the template to use. Select the Leaks template and click the Choose button.


Figure 4 - Selecting the Leaks template from Instruments

With the template chosen, you will see the main Instruments window with both the Allocations and Leaks templates shown. Since our application is running, we can attach to it from the Choose Target button and selecting the Attach to Process menu item, then navigate the list of running processes until we find memexplore. After memexplore has been selected, click the Record button in the toolbar. This starts the process of recording all object allocations as well as the leak detection procedure.


Figure 5 - Selecting the memexplore process from Instruments

With Instruments recording the memexplore application, switch to the Xcode console and select test 5 (allocRunReleasePoolWithPoolOverRetain). This test purposely performs an extra retain to the object so that it will leak. After the test is run, switch back to Instruments and click on the Leaks template header on the left side of the window. Within a a short time, the leaked object name should appear, along with the address and size. Clicking the third button of the view group in the toolbar will reveal the extended detail including the stack trace where the leak occurred. As we can see in the stack trace, the DDObject's allocWithZone: method is where the leak originated.


Figure 6 - Instruments showing the memory leak in memexplore

Summary

As we have seen, mismanaging an object's lifetime through its retain count can have ramifications for the health of your applications. Autorelease pools give us some convenience but even so, we must still be vigilant when balancing our retains and releases. Crashes are often caused by objects being released prematurely, and leaks are the result of retaining an object beyond its lifetime. It's times like these when Apple's Instruments can pinpoint the exact spot where the leak occurred, and we can take corrective action. For those of you who have started delving into Objective-C, these articles and the accompanying code should give you a basis for understanding memory management as well as a springboard to further experimentation.

Bibliography and References

Apple. Instruments User Guide. http://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/InstrumentsUserGuide.pdf

CocoaDev. DebuggingAutorelease.

http://www.cocoadev.com/index.pl?DebuggingAutorelease


Boisy G. Pitre lives in Southwest Louisiana and is the lead developer at Tee-Boy where he also consults on Mac and iOS projects with a variety of clients. He holds a Master of Science in Computer Science from the University of Louisiana at Lafayette. Besides Mac programming, his hobbies and interests include retro-computing, ham radio, vending machine and arcade game restoration, and playing Cajun music. You can reach him at boisy@tee-boy.com.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Recruit two powerful-sounding students t...
I am a fan of anime, and I hear about a lot that comes through, but one that escaped my attention until now is A Certain Scientific Railgun T, and that name is very enticing. If it's new to you too, then players of Blue Archive can get a hands-on... | Read more »
Top Hat Studios unveils a new gameplay t...
There are a lot of big games coming that you might be excited about, but one of those I am most interested in is Athenian Rhapsody because it looks delightfully silly. The developers behind this project, the rather fancy-sounding Top Hat Studios,... | Read more »
Bound through time on the hunt for sneak...
Have you ever sat down and wondered what would happen if Dr Who and Sherlock Holmes went on an adventure? Well, besides probably being the best mash-up of English fiction, you'd get the Hidden Through Time series, and now Rogueside has announced... | Read more »
The secrets of Penacony might soon come...
Version 2.2 of Honkai: Star Rail is on the horizon and brings the culmination of the Penacony adventure after quite the escalation in the latest story quests. To help you through this new expansion is the introduction of two powerful new... | Read more »
The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »

Price Scanner via MacPrices.net

May 2024 Apple Education discounts on MacBook...
If you’re a student, teacher, or staff member at any educational institution, you can use your .edu email address when ordering at Apple Education to take up to $300 off the purchase of a new MacBook... Read more
Clearance 16-inch M2 Pro MacBook Pros in stoc...
Apple has clearance 16″ M2 Pro MacBook Pros available in their Certified Refurbished store starting at $2049 and ranging up to $450 off original MSRP. Each model features a new outer case, shipping... Read more
Save $300 at Apple on 14-inch M3 MacBook Pros...
Apple has 14″ M3 MacBook Pros with 16GB of RAM, Certified Refurbished, available for $270-$300 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year warranty is... Read more
Apple continues to offer 14-inch M3 MacBook P...
Apple has 14″ M3 MacBook Pros, Certified Refurbished, available starting at only $1359 and ranging up to $270 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year... Read more
Apple AirPods Pro with USB-C return to all-ti...
Amazon has Apple’s AirPods Pro with USB-C in stock and on sale for $179.99 including free shipping. Their price is $70 (28%) off MSRP, and it’s currently the lowest price available for new AirPods... Read more
Apple Magic Keyboards for iPads are on sale f...
Amazon has Apple Magic Keyboards for iPads on sale today for up to $70 off MSRP, shipping included: – Magic Keyboard for 10th-generation Apple iPad: $199, save $50 – Magic Keyboard for 11″ iPad Pro/... Read more
Apple’s 13-inch M2 MacBook Airs return to rec...
Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices currently... Read more
Best Buy is clearing out iPad Airs for up to...
In advance of next week’s probably release of new and updated iPad Airs, Best Buy has 10.9″ M1 WiFi iPad Airs on record-low sale prices for up to $200 off Apple’s MSRP, starting at $399. Sale prices... Read more
Every version of Apple Pencil is on sale toda...
Best Buy has all Apple Pencils on sale today for $79, ranging up to 39% off MSRP for some models. Sale prices for online orders only, in-store prices may vary. Order online and choose free shipping... Read more
Sunday Sale: Apple Studio Display with Standa...
Amazon has the standard-glass Apple Studio Display on sale for $300 off MSRP for a limited time. Shipping is free: – Studio Display (Standard glass): $1299.97 $300 off MSRP For the latest prices and... Read more

Jobs Board

Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
*Apple* App Developer - Datrose (United Stat...
…year experiencein programming and have computer knowledge with SWIFT. Job Responsibilites: Apple App Developer is expected to support essential tasks for the RxASL Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.