Objective-C memory management how to understand Autorelease

Source: Internet
Author: User

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.

 
 
  1. [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.

 
 
  1. ClassA *Func1()  {       
  2.  ClassA *obj = [[[ClassA alloc]init]autorelease];  
  3.        return obj;    
  4.  }  

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:

 
 
  1. NSString * globalObject;
  2. -(Void) applicationDidFinishLaunching :( UIApplication *) application {
  3. GlobalObject = [[NSString alloc] initWithFormat: @ "Test"];
  4. NSLog (@ "Retain count after create: % d", [globalObject retainCount]);
  5. // Output 1. [globalObject retain];
  6. NSLog (@ "Retain count after retain: % d", [globalObject retainCount]);
  7. // Output 2.
  8. }
  9. -(Void) applicationWillTerminate :( UIApplication *) application {
  10. NSLog (@ "Retain count after Button click runloop finished: % d", [globalObject retainCount]);
  11. // Output 1. Button click loop finished, it's autorelease pool released, globalObject get released once.
  12. }
  13. -(IBAction) onButtonClicked {
  14. [GlobalObject autorelease];
  15. NSLog (@ "Retain count after autorelease: % d", [globalObject retainCount]);
  16. // Output 2. Autorelease is called, and globalObject is added as the current AutoreleaePool.
  17. }

Summary:Objective-C Memory Management: How to understandAutoreleaseI hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.