Memory Management in objective c
Have you read my last tutorial not? Click Here:Learn Objective C
Before going into the depth of Memory Management first clear the concept of memory.
What is memory?
Memory is used to store something in general.But technically there are flip flops at minor level which are set,reset or other according to need. You have heard about SR,JK flip flop all those are used to store data. But we developers are not directly link with flip flop. We just use programming which is compiled by compiler and then furthor at 0 or 1 level but i do not go in detail. I just go for memory Management in detail.
you know that Smart Phones are vary according to RAM memory sizes. Also IPhone has limilted memory space to run application. Also A good application is that which consume less memory and time to run but there will be some tradeoff occur between them. We are developers ,can do anything so it is in our hand how to manage memory which property to provide strong or weak reference.
In objective C there are two ways to manage memory.
1.) By manually
2.) Automatic Reference Counting (ARC)
1.) Manual:- By manually we can manage memory by tracking all the information of object when it allocated and when it release.
All the Alloc,Dealloc method are provided in the NSObject Class derived from foundation framework.
Manually it is very difficult to manage or remember when the object is created and release in larger projects having 100 objects.
I am using currently Xcode 6.1 so it is automatically ARC so if you want to test the below program then do following:
First click the project on left side.
Secondly go to bulid setting under apple llvm 6.0 compiler you have seen Automatice Reference Counting is checked or linked to Yes you need to set it no.
Note: but ARC is widely used.why we used it i will tell you later I suggest you always use ARC what the Xcode 6 set by default.
RetainCount is the counter to number of objects in projects.
I am using here Manually release example that is only for educational purpose you need to make project with ARC.
Example: Manually Retain and release is shown below.
Now we go through ARC.
What is ARC?
ARC is automatic reference counting as you have seen above manually retain and release you need to remember about retain and release of object. But now it is the work of compiler who do all thing for you. You do not need to remember when retain and release of object.Now you take care only writing programmes instead of caring about objects memory.
How Automatic Reference Counting Work?
As you have seen In my previos post i have used NSAutoReleasePoll class What is it. It is working like a Pool. Pool means contains collection of object. All the object present in NSAutoReleasePool gets message of automatically released. All the events are Processed when [pool drain]
occurs it provide release message to all object present in it. What about retain count? Retain count is added in heap after allocating memory to object.
I am not discussing here Example of Automatic Reference Counting. All the Previous program i have done through ARC.
ARC is just the work of compiler to retain and release object because objective c compiler is very smart.But just care which property is strong or weak reference if you have not read Click here:Property in Objective C
What happens if never care about memory ?
If you never release object in case of Manually Retain and Release then it causes your memory leak.Memory Leak means many memory addresses are consume spaces but currently not used of them. It causes your application damaged. More memory cycle are required in this case.
Source Code Available Here: Download Here
I hope you like my this tutorial. If you have any query you can mail me or comment here.
My next tutorial will be Multithreading in Objective C
Have you read my last tutorial not? Click Here:Learn Objective C
Before going into the depth of Memory Management first clear the concept of memory.
What is memory?
Memory is used to store something in general.But technically there are flip flops at minor level which are set,reset or other according to need. You have heard about SR,JK flip flop all those are used to store data. But we developers are not directly link with flip flop. We just use programming which is compiled by compiler and then furthor at 0 or 1 level but i do not go in detail. I just go for memory Management in detail.
you know that Smart Phones are vary according to RAM memory sizes. Also IPhone has limilted memory space to run application. Also A good application is that which consume less memory and time to run but there will be some tradeoff occur between them. We are developers ,can do anything so it is in our hand how to manage memory which property to provide strong or weak reference.
In objective C there are two ways to manage memory.
1.) By manually
2.) Automatic Reference Counting (ARC)
1.) Manual:- By manually we can manage memory by tracking all the information of object when it allocated and when it release.
All the Alloc,Dealloc method are provided in the NSObject Class derived from foundation framework.
Manually it is very difficult to manage or remember when the object is created and release in larger projects having 100 objects.
I am using currently Xcode 6.1 so it is automatically ARC so if you want to test the below program then do following:
First click the project on left side.
Secondly go to bulid setting under apple llvm 6.0 compiler you have seen Automatice Reference Counting is checked or linked to Yes you need to set it no.
Note: but ARC is widely used.why we used it i will tell you later I suggest you always use ARC what the Xcode 6 set by default.
RetainCount is the counter to number of objects in projects.
I am using here Manually release example that is only for educational purpose you need to make project with ARC.
Example: Manually Retain and release is shown below.
In Student.h
@interface Student : NSObject
@property(strong,nonatomic)NSString *StudentName;
-(void)printStudentName:(NSString *)StudentName;
In Student.m
@implementation Student
@synthesize StudentName=_StudentName;
-(void)printStudentName:(NSString *)StudentName
{
NSLog(@"The name of Student is:%@",StudentName);
}
In Teacher.h
@interface Teacher : NSObject
@property(strong,nonatomic)NSString *TeacherName;
-(void)printTeacherName:(NSString *)TeacherName;
In Teacher.m
@implementation Teacher
@synthesize TeacherName=_TeacherName;
-(void)printTeacherName:(NSString *)TeacherName
{
NSLog(@"The Name of Teacher is:%@",TeacherName);
}
In Main Section
#import "Student.h"
#import"Teacher.h"
Student *student1=[[Student alloc]init];
[student1 printStudentName:@"abc"];
NSLog(@"The Counter is:%d",[student1 retainCount]);
Teacher *teacher1=[[Teacher alloc]init];
[teacher1 printTeacherName:@"xyz"];
NSLog(@"The counter is:%d",[teacher1 retainCount]);
Student *student2=[[Student alloc]init];
[student2 retain];
[student2 printStudentName:@"def"];
NSLog(@"The Counter is:%d",[student2 retainCount]);
[student2 release];
NSLog(@"The Counter is:%d",[student2 retainCount]);
student2=nil;
The output of above program is shown below:
Now we go through ARC.
What is ARC?
ARC is automatic reference counting as you have seen above manually retain and release you need to remember about retain and release of object. But now it is the work of compiler who do all thing for you. You do not need to remember when retain and release of object.Now you take care only writing programmes instead of caring about objects memory.
How Automatic Reference Counting Work?
As you have seen In my previos post i have used NSAutoReleasePoll class What is it. It is working like a Pool. Pool means contains collection of object. All the object present in NSAutoReleasePool gets message of automatically released. All the events are Processed when [pool drain]
occurs it provide release message to all object present in it. What about retain count? Retain count is added in heap after allocating memory to object.
I am not discussing here Example of Automatic Reference Counting. All the Previous program i have done through ARC.
ARC is just the work of compiler to retain and release object because objective c compiler is very smart.But just care which property is strong or weak reference if you have not read Click here:Property in Objective C
What happens if never care about memory ?
If you never release object in case of Manually Retain and Release then it causes your memory leak.Memory Leak means many memory addresses are consume spaces but currently not used of them. It causes your application damaged. More memory cycle are required in this case.
Source Code Available Here: Download Here
I hope you like my this tutorial. If you have any query you can mail me or comment here.
My next tutorial will be Multithreading in Objective C


No comments:
Post a Comment