IOS Memory Management Summary
I have been learning iPhone programming for a month, and I am still a little unfamiliar with memory management. I want to organize some of my personal points that are concise and contain the following points:
Objective-C object generation and release
1. The objective-C object is generated on the heap. After the object is generated, a pointer is required to point to it.
Classa * obj1 = [[classa alloc] init];
2. The objective-C object will not be automatically destroyed after use. You need to execute dealloc to release space (destroy); otherwise, the memory will be leaked.
[Obj1 dealloc];
IOS Memory Management Mechanism
The memory management method of the iPhone uses the retaincount mechanism. when an object is created, the retaincount is set to 1. When an owner is added, the retaincount of the object increases by 1;
At the same time, when the object is release, the reserved count is reduced by 1, and one owner is lost. When the retention count is 0, the object is destroyed.
Main Interface for reference counting
1, alloc, allocwithzone, new (with initialization)
Allocate memory for the object, retaincount is "1", and return this instance
2, retain
Retaincount plus "1"
3, copy, mutablecopy
Copy an instance. If the number of retaincount is "1", this instance is returned. The obtained object is independent of other contexts (Clean object ).
4, release
The dealloc method of this object is called when retaincount minus "1" and is reduced to "0"
5, autorelease
Add this object to the autoreleasepool instance at the top of the autoreleasepool stack in the current context. As a result, the introduction of this object increases objective-C (non-GC management environment) from full manual memory management to semi-automation.
Objective-C memory management principles
We can classify the above interface into two types based on the Operation nature of retaincount,
Class A is a plus-one operation: 1, 2, 3
Class B: minus one operation: (delayed release)
The memory management principles are as follows:
1. The number of calls of Class A and Class B must be in the same format.
2. In order to ensure the good principle 1, the instance object is taken as the unit, and whoever has a is B, no two participate.
For example:
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
Nsobject * o = [[nsobject alloc] init]; // retaincount is 1
[O retain]; // retaincount is 2
[O release]; // retaincount is 1
[O autorelease]; // retaincount is 1
[Pool release]; // The value of retaincount is 0, triggering the dealloc Method
The value of retain count does not increase. Pay attention to memory leakage.
Classa * obj1 = [[classa alloc] init]; // retain COUNT = 1
Classa * obj2 = obj1; // retain COUNT = 1
[Obj2retain]; // retain COUNT = 2
[Obj1hello]; // output hello
[Obj1release]; // retain COUNT = 2-1 = 1
[Obj2hello]; // output hello
[Obj2release]; // retain COUNT = 0, object destroyed
Solve the problem! NOTE: If [obj2release] is not called, The retaincount of this object is always 1 and will not be destroyed, causing memory leakage.
Autorelease pool Principle Analysis
1. autoreleasepool is not born and needs to be created manually. Only when you create an iPhone project, xcode will automatically help you write it. The real name of autoreleasepool is NSAID utoreleasepool.
2. The nsmutablearray array is contained in the nutoreleasepool to save all objects declared as autorelease. If an object is declared as autorelease,
The system adds this object to this array. Classa * obj1 = [[classa alloc] init] autorelease]; // retain COUNT = 1, add this object to autorelease pool
3. When the NSAID utoreleasepool itself is destroyed, it traverses this array and each member in the release array. If the retaincount of the members in the array is 1,
After release, the retain count is 0, and the object is officially destroyed. If the retaincount of the members in the array is greater than 1, after release, the retain count is greater than 0,
This object is still not destroyed and Memory leakage occurs.
When memory is insufficient
By default, there is only one autoreleasepool. All objects marked as autorelease are destroyed only when the pool is destroyed. If you have a large number of objects marked as autorelease,
This obviously cannot make good use of the memory, which is easily caused by insufficient memory in iPhone memory-constrained programs.
For example:
Int main (INT argc, const char * argv [])
{
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
Int I, J;
For (I = 0; I <100; I ++)
{
For (j = 0; j <100000; j ++)
[Nsstringstringwithformat: @ "1234567890"]; // The generated object is autorelease.
}
[Pool release];
Return (0 );
} // Main
In the following code, does obj2 need to call dealloc?
Classa * obj1 = [[classa alloc] init];
Classa * obj2 = obj1;
[Obj1 Hello]; // output hello
[Obj1 dealloc];
[Obj2 Hello]; // can this row and the next row be executed?
[Obj2dealloc];
A: [obj1 dealloc]; The obj2 is actually destroyed, and an error will be reported during [obj2 Hello.