codescracker


objective c

Objective-C Memory Management



« Previous Tutorial CodesCracker Home »


Memory management is the process through which the memory of the objects are allocated when they are required deallocated when they are no longer required.

Memory management technique in Objective-C can be classified into these two types:

Automatic Reference Counting (ARC)

In ARC, the system uses the same reference counting system as MRR, except it inserts the appropriate memory management method calls at the compile time. In ARC, we needn't add release and retain method since that will be taken care by the Objective-C compiler. The underline process of Objective-C is still the same.

Manual Retain Release (MRR)

In MRR, we explicitly manage the memory by keeping track of objects on our own. This is implemented by using a mode, called as reference counting, that the Foundation class NSObject provides in conjunction with the runtime environment.

The only difference between the MRR and the ARC is that the retain and the release is handled by us manually in former while it's automatically taken care of later.

Objective-C Memory Management Example

Here is an example program, illustrates ARC in Objective-C:

/* Objective-C Memory Management - Example Program */
		
#import <Foundation/Foundation.h>

@interface MY_CLASS:NSObject
- (void)my_method;
@end

@implementation MY_CLASS
- (void)my_method
{
	NSLog(@"Hello, World!\n");
}
- (void)dealloc 
{
	NSLog(@"Object deallocated");
}
@end

int main()
{
	@autoreleasepool
	{
		MY_CLASS *sample_class = [[MY_CLASS alloc]init];
		[sample_class my_method];
		sample_class = nil;
	}
	return 0;
}

When the above code is compile and run, it will produce the following output:

2014-10-15 04:45:47.310 demo[8385] Hello, World!
2014-10-15 04:45:47.311 demo[8385] Object deallocated

Here is an example program, illustrates MRR in Objective-C:

/* Objective-C Memory Management - Example Program */
		
#import <Foundation/Foundation.h>

@interface MY_CLASS:NSObject
- (void)my_method;
@end

@implementation MY_CLASS
- (void)my_method
{
	NSLog(@"Hello, World!\n");
}
- (void)dealloc 
{
	NSLog(@"Object deallocated");
	[super dealloc];
}
@end

int main()
{
	MY_CLASS *sample_class = [[MY_CLASS alloc]init];
	[sample_class my_method];
	NSLog(@"Retain Count after initial allocation = %d", 
	[sample_class retainCount]);
	[sample_class retain];
	NSLog(@"Retain Count after retain = %d", [sample_class retainCount]);
	[sample_class release];
	NSLog(@"Retain Count after release = %d", [sample_class retainCount]);
	[sample_class release];
	NSLog(@"SampleClass dealloc will be called before this");
	sample_class = nil;
	return 0;
}

When the above code will compile and run, then it will produce the following output:

2014-10-15 04:39:52.310 demo[8385] Hello, World!
2014-10-15 04:39:52.311 demo[8385] Retain Count after initial allocation = 1
2014-10-15 04:39:52.311 demo[8385] Retain Count after retain = 2
2014-10-15 04:39:52.311 demo[8385] Retain Count after release = 1
2014-10-15 04:39:52.311 demo[8385] Object deallocated
2014-10-15 04:39:52.311 demo[8385] SampleClass dealloc will be called before this

« Previous Tutorial CodesCracker Home »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test