Memory management

Memory management in Objective C is different from languages like java, c# which have automatic memory management (garbage collection). Although garbage collection is supported in the runtime environment of Mac OS X, it is not supported in iOS. It has to be done explicitly in iOS.

Objective C uses “reference counting” as its main technique for memory management. Reference counting means keep track of number of times object is being referenced. As automatic garbage collection is not performed, developer has responsibility to declare object when needed and to delete it when it is no longer needed.

Let us see different methods used for memory-management.

1. Alloc:
i. It is used to allocate memory to objects.
ii. Ex:[NSString alloc], [NSArray alloc]
iii. alloc method sets object’s reference count to 1.
iv. When developer is done with object, it has to delete it using release method.
NSString *str = [NSString alloc]; or
NSString *str = [[NSString alloc] init];

2. new:
i. This method is short way of writing [[NSString alloc] init].
ii. [NSString new] is same as [[NSString alloc] init].

3. Copy:
i. It creates a copy of the object.
ii. It returns a new copy of object.
NSString *str1 = [str copy];

4. retain:
i. It increments reference count of object by 1.
[str retain];

5. release:
i. It is used to release memory of object.
[str release];

ii. It decrements reference count by 1.
iii. When reference count becomes 0 then object is deleted automatically and memory is freed.

[str retain];
[str release]; Reference count 1
[str release]; Reference count 0

6. autorelease:
i. It is used to automatically release memory of object when it is no longer used or at the end of its scope.

NSString *str = [[NSString alloc] init] autorelease]; or
[str autorelease];

ii. An autoreleased object is added to an autorelease pool.

iii. AutoreleasePool:
a. An autorelease pool is an instance of NSAutoreleasePool and defines a scope for temporary objects.
b. Any objects which are to be autoreleased are added to the current autorelease pool.
NSAutoreleasePool *pool = [NSAutoreleasePool alloc] init];
// temporary objects
[pool release];
c. All objects between pool allocation and release are automatically released when pool is released.
d. It is a simple way to manage memory of temporary objects.
e. Autorelease pools can be nested too. There is a stack similar as Last in First out.

7. Convenience methods:
i. These methods are static methods for allocating and initializing objects.

NSString *str = [NSString stringWithFormat:@”%@”, @”String”];

ii. This method allocates and initializes str object and adds it in autorelease pool.
iii. So str is automatically released when autorelease pool is popped.

How to do effective memory management:

i. Freeing object when it is no longer needed.
ii. Not freeing object when it is still in use.
iii. Release all objects in dealloc method.
iv. Use autorelease pool for temporary objects.

Tools to Debug Memory Problems:
1. Leaks instrument:
It is provided by Apple for finding out leaks in application.
2. Use NSZombieEnabled variable
i. NSZombieEnabled is a flag that developer can enable in Arguments.
ii. It will provide a warning when you try to access an object that has been deleted.
iii. Executables - Arguments- set NSZombieEnabled to YES

Tags: , , ,

Comments are closed.