In this blog post, we will introduce how to use memory management in OBJECTIVE-C. When a program runs, it does not release unused space memory when it is not in time. Then, the program will become more and more bloated, memory consumption will continue to rise. When we use it, we feel a lot of cards, eventually making the program run. Therefore, it is necessary to dispose of invalid memory in a timely manner.
How does an object experience a process when it is initially created and released to the last collection?
Includes: birth (implemented by Alloc or New method), survival (receiving messages and performing actions), making friends (by compounding and passing parameters to methods), eventually dying (released).
One, reference counting
At the time of object creation, cocoa used a technique called reference counting :
1) When an object is accessed , the value of the reference counter is incremented by 1, and a retain message can be sent to the object;
2) When the object's access is closed , the value of the reference counter is reduced by 1, and a release message can be sent to the object;
3) When the value of the reference counter is 0 , it means that the object is no longer accessed, the system memory it consumes will be recycled , will automatically send an dealloc message to the object, generally will override the Dealloc method;
4) to get the current value of the hold counter, you can send a retaincount message .
Below, we describe the following methods of Declaration and implementation:
First, create a new Retaintracker class, modify the declaration file for the class, and implement the method:
1 //RetainTracker.h 2 3 #import <Foundation/Foundation.h>45@interface retaintracker: NSObject6 -(ID) retain; 7 void ) release; 8 -(Nsuinteger) retaincount; 9 @end
1 //RETAINTRACKER.M2 3 #import "RetainTracker.h"4 5 @implementationRetaintracker6-(ID) Init7 {8 if(Self = =[Super init])9 {TenNSLog (@"init:retain Count of%lu.", [self retaincount]); One } A return(self); - } - the-(void) Dealloc - { -NSLog (@"Dealloc called. byebye!"); - [Super Dealloc]; + } - + @end
Then call the Retain,release,retaincount,dealloc method in the MAIN.M main function:
1 #import<Foundation/Foundation.h>2 #import "RetainTracker.h"3 4 intMainintargcConst Char*argv[])5 {6Retaintracker *tracker = [RetaintrackerNew];//Count =17 8 [tracker retain];9NSLog (@"Retaincount:%lu", [tracker Retaincount]);//Count =2Ten One [tracker retain]; ANSLog (@"Retaincount:%lu", [tracker Retaincount]);//Count =3 - - [Tracker release]; theNSLog (@"Retaincount:%lu", [tracker Retaincount]);//Count =2 - - [Tracker release]; -NSLog (@"Retaincount:%lu", [tracker Retaincount]);//Count =1 + - [tracker retain]; +NSLog (@"Retaincount:%lu", [tracker Retaincount]);//Count =2 A at [Tracker release]; -NSLog (@"Retaincount:%lu", [tracker Retaincount]);//Count =1 - -[Tracker release];//Count =0, Dealloc - - return(0); in}
The results of the operation are as follows:
Second, automatic release
We all know that when the object is no longer used, it should be released in time. But in some cases it's not easy to figure out when to stop using an object. It would be nice if I could release it automatically. Fortunately, there is an automatic release pool (autorelease) in cocoa. Careful friends can find that, after iOS5, each new project, in the main function, there is a @autoreleasepool method, which is the execution of the code is added to the automatic release pool.
The NSObject class provides a method called autorelease :
1 -(ID) autorelease;
This method pre-sets a release message that will be sent at some time in the future. When sending an autorelease message to an object, the object is actually added to the auto-free pool. When the auto-free pool is destroyed, a release message is sent to all objects in the pool.
So, next, we'll use the method of adding to the auto-release pool to modify the example above. The contents of the Retaincount class do not change, as long as the contents of the main function are modified:
1 /*Use Auto Release pool*/2 intMainintargcConst Char*argv[])3 {4NSAutoreleasePool *Pool;5Pool =[[NSAutoreleasePool alloc] init];6 7Retaintracker *tracker = [RetaintrackerNew];//Count =18NSLog (@"After new, tracker:%lu", [tracker Retaincount]);//Count =19 Ten [tracker retain]; OneNSLog (@"After retain, tracker:%lu", [tracker Retaincount]);//Count =2 A [Tracker autorelease]; -NSLog (@"After autorelease, tracker:%lu", [tracker Retaincount]);//Count =2 - the [Tracker release]; -NSLog (@"After release, tracker:%lu", [tracker Retaincount]);//Count =1 -NSLog (@"Releasing Pool"); - [Pool release]; //Destroy auto-release pool + - @autoreleasepool { +Retaintracker *Tracker2; ATracker2 = [RetaintrackerNew];//count = 1 at[Tracker2 retain];//Count =2 -[Tracker2 Autorelease];//count still = 2 - [Tracker2 release]; //count = 1 -NSLog (@"Auto Releasing pool."); - } - in return(0); -}
Operation Result:
The Tracker object, which is added to the auto-free pool through the autorelease message. When the pool auto-release cell sends the release message, the pool object's reference counter has a value of 0, and the Dealloc method is called to be destroyed. Causes objects in the auto-release pool to be destroyed along with them.
In the next blog post, we will continue to introduce the knowledge of memory management in OC and introduce cocoa's memory management rules.
OBJECTIVE-C Basic Tutorial Study notes (10)--the object life cycle of memory management