iOS Memory management methods:
ARC Automatic Reference Counting Auto Reference count
MRC Manual Reference Counting Manual reference count
Change Management methods:
Memory management issues:
1. Memory leaks: Objects that are no longer needed are not released.
2. Wild pointer: The object being used is released in advance.
Reference count:
1. There is a reference counter on each of the objects
2. When the object is born, the reference counter is 1
3. Reference counter when the object calls the Retain method +1
4. Reference counter when the object calls the release method-1
5. When an object reference count is 0 o'clock, the object is immediately recycled
Zombie object: An overly-disposed object.
iOS to optimize app performance, do not immediately deal with zombie objects, you can turn on Zombie mode.
Dealloc Method:
Called automatically when an object is retaincount=0.
Dealloc the release member variable object.
Dealloc must call the parent class method, [Super Dealloc] must be placed at the end.
Dealloc is automatically called by the system and cannot be called manually.
Dealloc optimization Self.dog = nil;
Memory Management principles:
Who applies, who releases.
Method of application: Alloc new copy mutablecopy retain
Release method: Release
@property modifiers add:
1. Default Assign
2, assign main decoration basic data type int long float double do not add any code to manage memory
3. Retain main modifier OC Object Add code to manage memory
Autorelease Automatic Release Pool
(i) Basic usage
(1) The object is placed in an auto-release pool
(2) When the automatic release pool is destroyed, all objects in the pool are released once
(3) The object itself is returned
(4) When the Autorelease method is called, the object's counter is unaffected (destroyed)
(ii) Benefits
(1) no longer need to care about the time the object was released
(2) No need to worry about when to call release
(iii) Use of attention
(1) Occupy large memory objects, do not use autorelease, should use release to precisely control
(2) Use Autorelease for objects that occupy less memory, with no significant impact
(d) Wrong wording
(1) Continuous Call multiple autorelease, release pool destroyed when the execution two times release (-1?). )
(2) After Alloc, the autorelease is called and then release is called.
(v) automatic release of the pool
(1) During the operation of the iOS program, countless pools are created that are present in the stack structure (advanced post-exit).
(2) When an object calls Autorelease, the object is placed in the release pool at the top of the stack
(vi) How the automatic release pool is created
(1) How iOS 5.0 was created before
NSAutoreleasePool *pool=[[nsautoreleasepool alloc] init];
`````````````````
[Pool Release];//[pool drain]; for Mac
(2) After Ios5.0
@autoreleasepool
{//start on behalf of Create auto release pool
·······
}//end represents destruction of the auto-release pool
Seven Autorelease Note
(1) in the system's own method, if the Alloc new copy is not included, the objects returned by these methods are autorelease, such as [NSDate Date];
(2) In development often write some kind of method to quickly create a Autorelease object, do not use the class name directly when creating objects, but use self
ARC Memory Management mechanism
(a) Arc's judging criteria:
As long as no strong pointer points to the object, the object is freed.
(ii) Pointer classification:
(1) Strong pointer: By default, all pointers are strong pointers, the keyword __strong
(2) Weak pointer: _ _weak keyword-decorated pointer
Declare a weak pointer as follows:
__weak person *p;
In arc, the weak pointer is emptied directly as long as the weak pointer is not in the object.
__weak person *p=[[person alloc] init];//unreasonable, the object once created is released, after the object is released, ARC automatically zeroed the pointer.
Instead of using retain in the property, the arc uses strong and no more [super Dealloc] in Dealloc.
@property (nonatomic,strong) Dog *dog;//means that the generated member variable _dog is a strong pointer, equivalent to the previous retain.
If it is a weak pointer, replace it with weak and do not need to add __.
(iii) ARC features Summary:
(1) not allowed to call Release,retain,retaincount
(2) Allow rewrite of dealloc, but do not allow calls to [super Dealloc]
(3) Parameters of @property:
Strong: Equivalent to the original retain (for OC object type), member variable is strong pointer
Weak: Equivalent to the original assign, (for OC object type), member variable is weak pointer
Assign: For non-OC object types (base type)
The MRC environment Get method is implemented:
Atomic down Get method
-(Keyborad *) KB {
[_kb retain];
[_kb Autorelease];
return _kb;
}
Nonatomic down Get method
-(Keyborad *) KB {
return _kb;
}
MRC Turn Arc:
MRC mixed with Arc:
Arcmrc
Under Options, select either do not use the compiled file, double-click it, enter-fno-objc-arc to
The Arc class can also be used in MRC projects.
Under Options, select the file you want to use to compile, double-click it, and enter
Circular reference: A-object strong pointer references a B object, a strong pointer to a B object references a object, and references to each other cannot be destroyed.
Workaround: Change one of the strong pointers to a weak pointer modified with weak.
iOS memory management (ARC,MRC)