Why is memory management needed? Because the memory of the mobile device is extremely limited, so the memory of each app is also limited, when the app consumes more memory, the system will issue a memory warning, you need to reclaim some no longer need to continue to use the memory space, such as recycling some of the unused objects and variables.
How is OC memory managed? OC memory management is different from other high-level languages such as C #, Java are through the garbage collection mechanism, OBJC memory management with reference counting mechanism. Managed Scope: Any object that inherits NSObject and is not valid for other basic data types.
What is a reference count? Reference counting mechanism: Within each object in OBJC there is a corresponding integer (retaincount), called "Reference counter", when an object is created after its reference counter is 1, when calling this object's alloc, retain, new, After the Copy method reference counter automatically add 1 on the original base (the method of calling an object in OBJC is to send a message to the object), when it calls the release method of the object its reference counter minus 1, if the reference counter of an object is 0, The system automatically calls the object's Dealloc method to destroy the object.
what is auto-referencing technology? Auto reference count: Refers to the technique of automatic counting of references in memory management. Apple official notes are as follows: Take the automatic Refrence counting (ARC) mechanism in objective-c and let the compiler do the memory management. "Setting arc to a valid state in the LLVM compiler eliminates the need to type the retain or release code again" in other words, the following conditions are not required to manually enter the retain and release codes. 1. Use Xcode4.2 or above, 2. Use LLVM compiler 3.0 or later, 3. Set arc to be valid in compiler option. If the above conditions are met, the compiler will automatically manage memory. Note? If you need to turn off arc in Xcode: Project Properties-build settings--Search "garbage" to find objective-c Automatic Reference counting set to No.
Memory management principles?
Principle 1
As long as someone else is using an object, the object will not be recycled;
As long as you want to use this object, then you should let the object reference counter +1;
When you do not want to use this object, you should let the object reference counter-1;
Principle 2 who created, who release
(1) If you create an object by Alloc,new,copy, you must call the release or Autorelease method
(2) You're not the one who created it.
Principle 3 who retain, who release
As long as you call the retain, no matter how the object is generated, you have to call release
Summary: the beginning and ends, there should be a reduction in addition. Once you have an object counter plus 1, you should make it last-1.
OBJECTIVE-C Memory management mechanism