Memory Management


In java,you are probably used to creating variables and having someone else clean up your mess after you are finished with them.Unfortunately on the iPhone you have to clean up after yourself.The NSObject class has some accounting stuff that keeps track of how many other objects are currently using this object (retain count).
Memory Basics :
       The devices doesn't have much memory.
--> Earlier devices contains 128 MB
--> iPhone 3GS,3rd generation iPod,iPad:256 MB
The memory will allocated to the variables in different blocks.But all Objective-C objects are heap based.
Global variables will stored in Data Segment.
Automatic variables (Defined in functions or methods) will stored in Stack.
dynamically allocated memory will stored in Heap.  

Reference Counting :
   Also called “retain counting” or “managed memory”.
Each object keep a count of how many other objects are using it.
When an object 's reference count becomes Zero the object is deallocated and its memory is returned to the heap.

Adjusting Retain counting:
The rule of managing memory is to make sure that,by the time program has finished executing the number of “ownership methods” called in an object will equal the number of “loss of ownership” methods.

Ownership methods:
Don't release anything you don't own.you own anything that (as following)
you call [myClass alloc] explicitly on
you explicitly [myObject retain]
you create an object with a method that has the word “copy” in it.

Loss-Of-Ownership methods:
           call [myObject autorelease] will release later
             call [myObject release]

Dealloc:
           If a release  message causes an object's retain count to drop to zero,the release  method invokes the object's dealloc method.
If the object has stored other objects in its instance variables,it must release them in the dealloc method.
Never directly call dealloc..you should always use release, and let they system decide when to deallocate.
The dealloc method must  invoke [super dealloc] as the last statement.It is NSObject's dealloc  method that actually returns the object's bytes to the heap.

Memory Management Rules ( do):
        Release an object you've created (with alloc,copy, or new)or retained when you're done with it.
Override dealloc to release objects ( e.g.:instance variables) as necessary when your object is to be destroyed this includes properties declared with a copy or retain attribute.

Memory Management Rules ( con't do):
         When the autorelease  pool is drained , a release message is sent to each object in the pool for each time the object has been autoreleased.
If you no longer need an object from within a method but need to return it,send it an autorelease message.
If you need an object to survive deallocation of the autorelease pool send it a retain message.
If you create an object using alloc,copy, or new you are responsible for releasing it.
You don't have to worry about releasing objects returned by framework methods.

Ex:
NSArray *namesArray = [[NSArray alloc] init];

You must release the object ,otherwise it is a leak.
[namesArray release];

release decrements the object's reference count.if the new reference count is Zero,the object is deallocated.

if we are trying to release same object one more time the app will crash.Because of the count is equal to Zero.then automatically dealloc the memory.When the retain count = 0 the object's “dealloc” method is called and object is freed..

Convenience Methods:
Many classes in cocoa are static methods used to allocating and initializing objects directly. You are not the owner of the returned objects and they are deleted automatically when the autorelease pool is popped.

Eg:
NSNumber *aNumber = [[NSNumber alloc] initWithFloat:5.0f ];

This creates a new instance of NSNumber,initializes it with “initWithFloat” method and parameter 5.0f. Now the reference count is 1.so you are the owner of aNumber and you must release it when you are done.

Using convenience method

Eg:

NSNumber *aNumber  = [NSNumber numberWithFloat:5.0f ];

Here also the reference count is 1,but you are not the owner of aNumber and should not release.The owner is the NSNumber class and the object will be deleted automatically at the end of current scope defined by autorelease pool.

Autorelease Pools:
An autorelease pool is an instance of NSAutoreleasePool and defines a scope for temporary objects.Any objects which are to be autoreleased are added to the current autorelease pool.when the autorelease pool is released all objects that were added to it are also automatically released.

Ex:

+(Employee *)emp
{
Employee *aEmp = [[Employee alloc] init];
return aEmp;
}

}

@interface MemoryManagementTC_ViewController : UIViewController {
IBOutlet UILabel *stringLbl1;
}
@end


#import "MemoryManagementTC_ViewController.h"

@implementation MemoryManagementTC_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

int a = 5;
NSString *string1 = [[NSString alloc] initWithFormat:@"Value of a is : %d",a];
stringLbl1.text = string1;
NSLog(@"retain-count of string1 is : %i",[string1 retainCount]);

[string1 release];
NSLog(@"retain-count of string1 is : %i",[string1 retainCount]);

}

output:

retain-count of string1 is : 2
retain-count of string1 is : 1



Reactions: