Proper memory management by an application is key to stability of an application. Doing it incorrectly can cause a myriad of bad things to happen! The side effects can include corrupting application data, crashing the application, locking up use of the computer or device, or even crashing the system or causing it to reboot.  Thus, memory management concerns are always in the back of the mind of every programmer (and, if they are unfortunate, the front of their mind), especially when they learn a new language. This article introduces programming issues related to memory management. In my next article I will get more technical with best memory management practices in programming iOS/CocoaTouch View Controllers.
In older, procedural programming languages that allow memory allocation (non-object based languages such as C or  Pascal), managing memory was pretty straight forward:
[sourcecode language=”c” light=”true”]
int appFunction(int size)
{
int *array = malloc(size * sizeof(int));
â‹®
free(array); // Done with the dynamically allocated memory
} // appFunction()
[/sourcecode]
Example: C-language memory allocation.
With object-oriented programming, application designers tend to structure their designs as a fluid interaction of objects. The syntax of object-oriented languages (such as C++ or Objective-C) helps programmers to avoid memory management mistakes easier than procedural languages, particularly in more complex application designs.
Since Objective-C was invented along with the APIs and frameworks used for environments like iOS and Mac OS X, Apple assumes its use with a class hierarchy and, for all practical purposes, a dependence on the NSObject
base-class. This class determines a whole gamut of conventions that are nearly indistinguishable from the Objective-C language itself, so it is hardly worth discussing the use of Objective-C without NSObject
. Â While it looks complex, the important thing is that the allocation and deallocation is handled at well-known locations in the code that will be used for other purposes, the initializer and deinitializer.
[sourcecode language=”obj-c” light=”true”]
– (id)init:(int) size
{
self = [super init];
if (self)
_array = [[NSArray alloc] init];
return self;
} // -init
â‹®
– (void)dealloc
{
[_array release];
[super dealloc]; // Done with the dynamically allocated memory
} // -dealloc
[/sourcecode]
Example: Objective-C memory handling
Just as, with C, where you need to internalize when you should call malloc()
and free()
, with Objective-C  you should internalize “object ownership” and when to send retain
and release
. These retain
/release
messages (sending a message is analogous to a function or method call, in other languages) implement reference-counting—a way of keeping track of how many objects are referring to the object.
Internalize this rule: If you do alloc
, retain
, or copy
on an object, you own it and it is your job to release
it… otherwise it isn’t.
In Objective-C, objects allocate other objects for its use or other’s use. These allocations are memory allocations. Cocoa (used in Mac OS X) and CocoaTouch (the Cocoa variant used for iOS) programming frameworks assume a clear understanding of who owns an object. Any number of objects can retain ownership another, either explicitly or implicitly. If one object allocates another, then there is an implicit ownership of that object.
stringOwner
implicitly owns the allocated object, so it must release
it.
[sourcecode language=”obj-c” light=”true” highlight=”1,2,3″]
NSString *stringOwner = [[NSString alloc] init];
â‹®
[stringOwner release];
[/sourcecode]
tempString
does not get ownership, but it can use it for the duration of the method.
[sourcecode language=”obj-c” light=”true” highlight=”1,2,3″]
NSString *tempString = [NSString string];
[/sourcecode]
stringOwner
has claimed ownership via retain
, so it must release
it.
[sourcecode language=”obj-c” light=”true” highlight=”1,2,3″]
NSString *stringOnwer = [[NSString string] retain];
â‹®
[stringOwner release];
[/sourcecode]
If an object is passed as a parameter to another method, it is possible that the other method may claim ownership of the object. In this case, the sender (AKA “caller” in other languages) can release the object if it won’t need it, later.
[sourcecode language=”obj-c” light=”true” highlight=”1,2,3,4,5,6″]
NSString *stringOnwer = [[NSString string] retain];
â‹®
[existingArray addObject:stringOwner]; // array is also, now, an owner
â‹®
[stringOwner release]; // Not needed by this method (or class any longer)
[/sourcecode]
The problem is that, despite Apple’s simple set of rules (or, better yet, the Rules of Thumb), it is often not clear which of their APIs retain ownership of the objects passed in, even by reading the official documentation. I usually find that if I am at all unsure, I need to google it—which often leads to the most reliable advice coming from Stack Overflow.
Apple must know this because they provide their excellent (though not perfect) Instruments tool that works with their Xcode development environment to help ferret out memory bugs and leaks.
Next, I will write about memory usage as it relates to iOS CocoaTouch View Controllers (because that also isn’ve very apparent).
Resources — The Basics
Why We Switched to Facebook Comments
Every couple of weeks we participate in a Founder Institute master class to continue discussions and keep forward momentum for our startup. The subject for the last call was user acquisition. We went over the various tactics on how to drive engagement through social media channels like Facebook fan pages, Twitter, Quora, Youtube, and commenting on blogs related to our business. Each startup participates in a different mix of social outlets, it really depends on where the customers for your company hangout, a business to business (B2B) company may not require a YouTube or Facebook presence. B2B startups may spend more time on Linkedin discussions or Quora answering questions and establishing themselves as subject matter experts in an area related to their product offering.
Driving Awareness on Facebook
After the call, it was clear we needed to switch from using the Disqus commenting system to Facebook comments. Migration will be easy since we are fairly new and we haven’t received many comments using Disqus. While no one wants to be dependent on Facebook for all of the social aspects of their web properties, this is a case where it made sense for us to move to Facebook comments. Moms with toddlers who are on Facebook are our launch target and we want to make sure every comment made on the Hand Things Down blog shows up on their walls to further drive engagement with our brand.
Where does your customer hangout online?
On Wednesday night, I was at a Social Media Planning Meeting and Jeff Moriarity said one of the pros for using Disqus is conversation around your brand from all over the web is aggregated in one place. Here are some questions your startup will need to consider; Where does your customer hangout? What do they read? What videos do they watch? Are they more likely to use Facebook or Twitter? You also need to keep in mind that if your target customer is only on Facebook, perhaps Facebook commenting will help them move from being a lurker to an engaged member of your community.
Your company can listen for conversations around the web using a different tool. We are using Google Alerts to listen for any mentions of our brand and actively retweet or push original content on Twitter and our Facebook fan page.
Posted in Commentary