Now it's still a dish. If there's any mistake, I hope to correct it!
If reprint, please indicate Source Address: http://blog.csdn.net/shinilaobababa/article/details/8453434
First, let's take a look at the golden rule of Memory Management in OC.
If you use alloc, [mutable] copy, and retain for an object, you must use the corresponding realtime or autorelease. (The golden rule should have a golden background)
Objective-C's memory management mechanism is flexible, that is, it can be completely manually managed, or it can be upgraded to a semi-automated memory management mode using autoreleasepool.
1. Memory Management Mechanism in cocoa-reference count
Reference counting is also called retain counting. The value of reference count indicates that several other objects are using it.
Each object has a reference count
When an object is created, the reference count value is 1.
When a retain message is sent, the reference count of this object is increased by 1, and the reference count of this object is 2.
When this object sends a release message, the reference count of this object is reduced by 1.
When the reference count of an object is 0, the system automatically calls the dealloc method to destroy the object.
Dog * dog = [[DOG alloc] init]; // The reference count is 1 nslog (@ "% d", [DOG retaincount]); [DOG retain]; // The reference count is changed to 2 [Dog release]; // The reference count is changed to 1 [Dog release]; // The reference count is changed to 0, and the system automatically calls the dealloc method to destroy the object, reclaim memory
Reference count is the only reference for memory collection of instance objects
Retaincount is the only basis for objective-C to manage object references. After the instance's release method is called, this attribute is reduced by one. The dealloc method of the object to zero is automatically called to recycle the memory. That is to say, we should never manually call the dealloc method of the object.
Dealloc Method
When the reference count of an object is 0, the system will automatically call the dealloc method to recycle the memory. It is generally written as follows:
-(Void) dealloc
{{
[Super dealloc]; [person release];
} [Super dealloc];
}
Why call the dealloc method of the parent class?
Some instances of the subclass inherit from the parent class. Therefore, we need to call the dealloc method of the parent class to release these objects owned by the parent class.
In general, the call order is that when the child class object is released and then the instance owned by the parent class is released, this is the opposite of calling the initialization method.
2. object ownership
When an owner (which can be any OC object) performs the following action, it has the ownership of an object.
(1) If an object is created or copied, the object is owned, that is, when the following keywords are included:
Alloc, allocwithzone:, copy, copywithzone:, mutablecopy, mutablecopywithzone:
(2) If you do not create or copy an object, but retain the reference, you also have the right to use the object.
Retain
(3) If you have the ownership of an object and do not need an object, you need to release it.
Release, autorelease
3. Usage of Auto Release pool
(1) The autorelasticsearch pool in cocoa can automatically release the red loyal objects. The nsobject class provides an autorelease message. When we want an object to send the autorelease message, this object will be released with the destruction of the release pool. To release objects from the automatically released pool, you must first perform the following operations:
// Create an automatic release pool @ autoreleasepool {// write method after the pool object 5.0}
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init]; // write [pool release] before the pooled object 5.0;
The automatic release pool is implemented in the form of stacks. When an object calls the autorelease method, the object will be added to the stack top of the automatic release pool. For objects that have sent the autorelease message, when the automatic release pool is destroyed, the automatic release pool will send a release message to these objects to release them.
(2) differences between the release and drain messages sent to the Auto Release pool
When we send a release message to the Auto Release pool, it will send a release message to each object in the pool that sends the autorelease message, and it will destroy itself. When a drain message is sent to it, only the objects in it will be released without destroying itself.
4. auto reference counting
When you compile a program, you can automatically manage the memory. It automatically adds the memory control code to control the object lifecycle, greatly simplifying the memory management steps, the principle of arc content management is that the compiler will automatically insert retain, release, and autorelease messages in appropriate places. However, note that the version is on ios4 or later (not very sure ). To use this method, you can select this option when creating a project in xcode,
When creating a Mac command line program:
When creating an iOS project:
5. Garbage Collection Mechanism
Similar to the garbage collection mechanism in Java, objective-C also provides a garbage collection mechanism after 2.0, but it is not supported in IOS. But what we need to understand is that the garbage collection mechanism is not an arc, and the ARC also needs to manage the memory, but it only implicitly manages the memory.