Objective-C Memory ManagementHow to understandAutoreleaseIs the content to be introduced in this article, mainly to learnAutoreleaseFor details about how to use it, refer to this article. If you can really understandAutoreleaseSo you understandObjective-COfMemory Management.
Autorelease actually only delays the call to release. For each Autorelease, the system only places the Object in the current Autorelease pool. When the pool is released, all objects in the pool will be called Release. Bytes
In fact, all objects returned by constructors such as [NSString stringWithFormat:] are autorelisted.
Autorelease pool is used to avoid frequent application/release of memory ). This should be relatively easy to understand.
Conclusion: Pay attention to the life cycle of the Autorelease pool and understand the Runloop to avoid using the object after it is released.
- [NSString stringWithFormat:]
Objects returned by such functions do not need to be release by themselves. They are already autorelisted. If you want to use them as a global object, you must retain it yourself and release it again.
Why do we need Auto release?
Many C/C ++ programmers will say that this auto release has something to do. Just like C/C ++, it is totally uncontrollable to apply for it and release it, this auto relase is completely uncontrollable, and you don't know when it will be truly release. In my understanding, each function is responsible for the objects applied for, applied for, and released by itself. The caller of this function does not need to care about the management of internal application objects. In the following example, the caller of Func1 does not need to care about the release of obj.
- ClassA *Func1() {
- ClassA *obj = [[[ClassA alloc]init]autorelease];
- return obj;
- }
In the Iphone project, you will see a default Autorelease pool, which is created when the program starts and destroyed when the program exits. According to your understanding of Autorelease, isn't all objects in the autorelease pool release when the program exits, what is the difference between this and Memory leakage?
The answer is that for each Runloop, the system will implicitly create an Autorelease pool, so that all release pools will constitute a stack structure like CallStack. At the end of each Runloop, the Autorelease pool at the top of the current stack will be destroyed, so that every Objective-C in this pool will be release.
So what is a Runloop? A ui event, Timer call and delegate call, will all be a new Runloop. Example:
- NSString * globalObject;
- -(Void) applicationDidFinishLaunching :( UIApplication *) application {
- GlobalObject = [[NSString alloc] initWithFormat: @ "Test"];
- NSLog (@ "Retain count after create: % d", [globalObject retainCount]);
- // Output 1. [globalObject retain];
- NSLog (@ "Retain count after retain: % d", [globalObject retainCount]);
- // Output 2.
- }
- -(Void) applicationWillTerminate :( UIApplication *) application {
- NSLog (@ "Retain count after Button click runloop finished: % d", [globalObject retainCount]);
- // Output 1. Button click loop finished, it's autorelease pool released, globalObject get released once.
- }
- -(IBAction) onButtonClicked {
- [GlobalObject autorelease];
- NSLog (@ "Retain count after autorelease: % d", [globalObject retainCount]);
- // Output 2. Autorelease is called, and globalObject is added as the current AutoreleaePool.
- }
Summary:Objective-C Memory Management: How to understandAutoreleaseI hope this article will help you!