Common iOS memory mistakes developers commit

Leave a comment


Some of the common mistakes an iOS developer does are provided below:

1. Missing dealloc method as part of  each Class

2. Missing [super dealloc] methods call in dealloc method.

3. Leaving NSLog statements in production ready code. Too much NSLog can consume lot of memory and your app will throw memory warning very soon.

4. Not being aware of the power of iOS debugger commands to nail the crashes. Environment variables NSZombieEnabled, NSDebugEnabled, MallocStackLogging can help you a lot in finding the reasons behind crashes.

5. When using the above commands, forgetting to disabled in the production code.

6. Not leveraging the power of accessors. To leverage their power read this document https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html

7. Not setting pointers to nil after releasing. Even though this is not necessary,  doing this is will help us in avoiding any inadvertant usage of this object variable, unless it is reassigned with any other object.

8. Trying to use retainCount of objects created.  Since there is no  1 to 1 correspondence between the retain count and calls to  retain/release, it is better we refrain from sing this.  This link might be use ful .http://www.friday.com/bbum/2011/12/18/retaincount-is-useless/

This list will be a living list and I will keep updating it.

Advertisements

Memory Management in iPhone – 3

Leave a comment


I referred to autorelease in the first and second part of the article. Today we will see what autorelease pool is all about.

Before directly getting in to Autorelease pool, lets see a new constructor that generates the autoreleased objects.


Convenience constructors:

Those who have worked with cocoa or coca-touch must have come across the following code:

Line 1:NSString *string = [NSString stringWithCString:”Hello”];

What makes it different from the typical :
Line 2: NSString *stringObject = [[NSString alloc] init];

These methods are called convenience constructors and used for creating temporary objects. Line 1 is equivalent to the following line:
Line 3: NSString *stringObject =[ [[NSString alloc] init] autorelease];

That is, the “convenience constructor” creates an object that is sent an autorelease message. In our case the string created in Line 1autorelease works by making use of an object named autorelease pool.So what is autorelease pool?


Autorelease pool


An autorelease pool is an instance of NSAutoreleasePool.
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

An autorelease pool contains other objects that have received an autorelease message. So, when the autorelease pool is deallocated, it sends a release message to all objects that are contained in them. Once you send an object an autorelease message or get an object from a convenience constructor, never bother about releasing it. What happens when you create a second autorelease pool in your application? Autorelease pools are arranged in stack. When you send a autorelease message, the object is added to the current topmost pool of the current thread.

Why should we go for an another autorelease pool?

In iPhone development context, we might need to create autorelease pool at the following scenarios:

  • When you spawn a secondary thread, in addition to the main thread of your Application. It is not necessary if this thread call does not involve any cocoa function calls.

     

  • If you write a looping logic where you create many temporary objects, you may create an autorelease pool inside the loop to dispose of those objects before the next iteration. This would help reduce the maximum memory footprint of the application.


Drain or Release:
To release a pool, in a non garbage collected environment, both drain and release serves the same purpose. But Apple suggests us to use drain.

Memory Management in iPhone – 2

1 Comment


As part of first article we learned about object ownership. So how does the operating system keep track whether an object needs to be released or not? It is done using reference counting  mechanism– retainCount of each object.(Apple suggests us not to use reatinCount to directly make any decision regarding memory management). Each object in Cocoa has retainCount as a property.

Lets see what happens to retainCount of an object when you do some actions that have impact on retainCount:

  • When you create an object(new or alloc, copy or mutablecopy), it has a retain count of 1.
  • When you send an object a retain message, its retain count is incremented by 1.
  • When you send an object a release message, its retain count is decremented by 1.
  • When you send a autorelease message to an object, its retain count will be decremented by 1(not immediately as in case of release but some time in the future – We will discuss this in detail some time in future)

An object is deallocated when the retainCount reaches 0. Deallocation involves invocation of dealloc method of an object .(dealloc frees objects own memory and any other object holds).

And remember never to invoke dealloc method of an object.

Objects created with any method other than new, alloc, copy , mutablecopy, have a retain count of one but will be autoreleased. If you need to keep them in memory, you need to explicitly retain them.

NSString *stringObject = [[NSString alloc] init]; // retain count incremented by 1

[stringObject retain]; // retain count incremented by 1

[stringObject release]; // retain count decremented by 1

[stringObject autorelease]; // retain count will be decremented by 1, not guaranteed to happen //immediately as in case of release

So lets wind up the discussion at this state and will meet in next article.

Memory Management in iPhone

4 Comments


Hi Friends,

We will have a series of articles on memory management in the days ahead. And as a primer I would give you the basics of memory management  in iPhone Development.

Now let’s get in to the topic.

We may be aware of the fact that resources must be managed effectively and it applies to memory also. So the rule of thumb is to ensure that objects you have created are disposed when they are no longer necessary.

Memory Management in objective -C (cocoa-Touch)can be better understood when we know about Object Ownership. This

What is Object Ownership?

You become owner of an object when either

  • you create an object by using a method whose name begins with alloc or new.
  • you create an object by using a method which contains a keyword copy.
  • you send an object a retain message.

What should you do when you become owner of an object? You have to give up your control over that particular object using either release or autorelease . You have no rights to relinquish control over objects that you don’t own.

Lets meet with the next article…