Automatic release and convenient method for iPhone Memory Management

Source: Internet
Author: User

We have discussed how to effectively control "object ownership and reference count" in the memory management of the iPhone software. In this article, we will introduce "Auto release) convenience method ).

BKJIA recommendation topic: iPhone application development

Sometimes when an object is created by an owner, the pointer of the object is immediately passed to other owners. At this time, the Creator does not want to own this object any more, but if a release message is sent to it immediately, this object will be released immediately-so that other owners have not yet had time to retain this object. The solution to this dilemma is to send an autorelease message to the object: The Creator no longer owns the object; the object becomes an automatically released object, but will not be immediately released; other owners can retain or copy the object for time and become its unique owner.

Let's take a look at the example code listing 3-1 for automatic release ). An owner first uses the alloc method to create an object. At this time, the owner owns this object and the reference count of the object is 1. Then, the owner Automatically releases the object. The owner has abandoned the ownership, but the reference count of the object is still 1 for a period of time. We can see another benefit of automatic release: You will not cause memory leakage because you forget to send a release message to the object later.

Code List 3-1

 
 
  1.  
  2.  
  3. -(Object*)returnAutoreleaseObject {  
  4.  
  5. Object* obj = [[Object alloc] init];  
  6.  
  7. return [obj autorelease];  
  8.  
  9. }  

There is a major constructor method related to automatic release. The objects constructed by them are directly released. This kind of constructor method is called a convenient method. For example, the string in the following sentence is an automatically released object, and stringWithFormat is a convenient method.

 
 
  1. NSString* string = [NSString stringWithFormat:@”autoreleaseString”]; 

Let's take a few examples of convenient methods to facilitate your future development.

1. NSArray's arrayWithObjects: And arrayWithArray :.

2. imageNamed: Of the UIImage :.

3. numberWithBool of NSNumber.

Now we have explained that the autorelease method will release an object after a period of time, during which we can safely use this object. So how long is this time? We need to learn more about the automatic release mechanism before answering this question.

Let's take a look at the automatic release pool. The Auto Release pool is an instance of the NSAID utoreleasepool, which contains the object that receives the autorelease message. When an automatic release pool is destroyed by dealloc), it will send a release message to each object in the pool. If you send an autorelease message to an object multiple times, when the Auto release pool is destroyed, this object will also receive the same number of release messages ). It can be seen that an automatically released object can survive at least when the automatically released pool is destroyed.

When will the automatic release pool be created and destroyed? At the beginning of each event cycle, the system automatically creates an automatic release pool. At the end of each event cycle, the system automatically destroys the automatic release pool. Generally, you can understand that when your code continues to run, the automatically released pool will not be destroyed, during this period, you can also safely use the automatically released objects. When your code runs and starts waiting for user input or other events, the automatically released pool will be released, and objects in the pool will receive a release message, and some may be destroyed.

So far, I believe you have a general understanding of the automatic release mechanism. Automatic release instead of direct release can help you save some code and increase development speed. However, it has a direct disadvantage: It delays the release of objects, and will occupy a large amount of memory resources when there are a large number of automatically released objects. Therefore, you need to avoid releasing a large number of objects automatically. In addition, in the following two cases, you need to manually create and manually destroy the automatic release pool:

1. When other threads are enabled outside the main thread: the system will automatically generate and destroy the automatic release pool in the main thread.

2. When you create a large number of automatically released objects in a short period of time: timely destruction helps to effectively use the limited memory resources on the iPad.

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.