1. Memory Management Overview
1.1 What is memory management : Memory management is a part of the common resource management in program design, and each computer system can use a limited amount of memory.
1.2 Why to use memory management : When our program runs to the end, the operating system will reclaim its memory from our program. However, it will always consume memory as long as the program is still running. If you do not clean up unused memory in a timely manner, the memory will eventually be exhausted. Every program uses memory, and we have to make sure that we allocate memory when we need to, and release the memory when the program runs. If we only allocate without releasing memory, a memory leak will occur.
1.3 Reference count :
1.3.1 You have a reference to an object, such as Alloc,copy or retain, after you have manipulated it.
1.3.2 When you don't need this object, you should release your references to him.
1.3.3 You can not perform a release operation on an object that you do not reference.
Once iOS is out of memory, the iOS platform notifies all apps that are already running, whether it's a foreground app or a background-pending app, and will receive memory warning Notice once the app receives memory Waring notice should reclaim large memory-intensive variables.
1.4 Memory alert processing :
1, the program received the system to send memory waring
2, the program frees up the memory of the larger.
3. The system recycles the Atutorelease object created by this program.
2. Memory Management
2.1 Manual reference count and auto-release pool
Manually manage the range of memory : Any object that inherits NSObject, and objects created with methods prefixed with alloc, copy, Mutablecopy, and new are not automatically freed, then manual management is required.
reference count : object is created by default with 4 of its own reference counter, and the default value is 1, when the reference count value is 0, the object will be destroyed by the system.
Get reference count : Send Retaincount message to object get current reference count, return value is unsigned long integer, can be printed using the following statement:
NSLog (@ "%zi", [person Retaincount]);
Get references :
Alloc: Allocates memory for a new object and resets its reference count to 1. Call the Alloc method and you have ownership of the new object.
Retain: Is the reference count of the object +1, and gets the reference to the object.
To release a reference :
Release: is the reference count of the object minus 1.
Autorelease: The reference count of the object is reduced by 1 at some point in the future, and the current reference count does not change.
When the reference count is 0, the system automatically calls the Dealloc method that inherits from NSObject, frees the memory, and then, in order to be able to release instance variables or other objects created or persisted by the object, requires a Dealloc method, but it is not recommended to call the Dealloc method directly. Because this is called by the system.
Memory Management principles :
* Who creates, who releases, if you create an object through Alloc, new, or copy, then you must call release or Autorelease.
* Generally, objects created by methods other than Alloc, new, or copy are declared autorelease.
* Who retain, who release. As soon as you call the retain, no matter how the object is generated, you call release.
Dark Horse programmer _ios Development _objective-c Learning Notes _ memory Management