Objective-C 2.0 with cocoa Foundation-9, memory management

Source: Internet
Author: User

9.1 Preface

Memory management is very important in objective-C 2.0. If memory management is well performed, the most intuitive response lies in yourProgramCrash. If your program is robust and stable, be sure to understand the memory management.

9.2 how is objective-C 2.0 a memory mechanism?

In objective-C 2.0, memory management is mainly based on a release Count value to determine whether the system wants to recycle the memory. When the retain count of an object is equal to 0, the system recycles this memory. So when we want to release a project, we only need to make its retain count equal to 0. When we want to hold this segment of memory so that we can continue to use it later (how to use it will be mentioned later), you only need to ensure that the retain count> 0 before you want to use it. Why does apple do this? Wait.

First, explain how retain count operates. Suppose we have a class A: nsobject, And then we declare a variable A * A without worrying about whether it is a temporary variable or a class attribute variable. At this time, its retain count is equal to 0; when we create a memory space for this variable, we will use the alloc function of the nsobject class: A = [A alloc]; at this time, the retain count of a will be + 1, the functions that make retain count + 1 not only have alloc, but also [A retain] and [A copy]. When will these two functions be used and their differences, it will be discussed later. To make retain count-1, you need to call the function [A release]; then write a stringCodeTo intuitively express the changes of retain count:

 

# Import " A.h "

/** Omitted **/

-( Void ) Retaincountdemo
{
A *; // Retain count of A is 0 A = [A alloc]; // Retain count of A is 1
[A retain]; // Retain count of A is 2
[A release]; // Retain count of A is 1
A * a1 = [A copy];// Retain count of A1 is 1, and a's is 1 too
[A release]; // Retain count of A is 0, and it will be dealloced
[A1 release]; // Retain count of A1 is 0, and it will be dealloced
Return ;
}

9.3 what is the difference between copy and retain?

The difference between copy and retain lies in their literal meanings. The difference is that one is direct reference (for example, Object Reference), and then retain count + 1, the other is the use of replication (for example, the use of strings), The retain count + 1 of the Copied object A1.

9.4 what are their uses?

Objective-C 2.0 is an object-oriented language. By using retain count, developers can create and use their respective modules and then "release" them ", don't worry about the crash caused by using this variable in other modules at the same time. The most typical and commonly used request is the request in our nsurlconnection class. You do not know when to release this asynchronous request because you cannot determine when the request can be returned, so by using retain count, you only care that you have created an nsurlconnction. After setting the receiving object, you can release it after sending the request, there is no need to worry about problems such as memory leakage. When sending a request, nsurlconnction changes the retain count to 2. When you call release in your function, the retaincount is changed to 1, when the callback function is completed when the request is returned, the release is automatically called once. At this time, the retain count of nsurlconnction is changed to 0 and released.

Through the above example, we can find that this is actually a mechanism for delayed release (well, this word is my own ). According to this, we can solve the problem of sharing many objects without knowing who to manage the memory. Apple's practice is to manage yourself and your own alloc + retain several times, the number of Release requests. Therefore, the most basic thing about memory management is to manage yourself!

 

9.5 Is memory management simpler?

Yes! But it is not recommended.

Method 1: autorelease function. You should know through the function name that this is for your automatic release. However, it should be noted that this function will only automatically help you release1 times. If you use retain in the middle, manually release the function. At the same time, this has a fatal drawback. It is very dangerous if you want to use autorelease to create an object in Class A and pass it to Class B, autorelease manages the memory based on the automatic release pool provided by the system. The system checks the objects in the cast pool at intervals and releases unused objects. When you pass it to B, your program will crash again before it can be used and is automatically released. Therefore, autorelease is usually used in local objects.

Method 2: iOS 5 arc (automatic reference counting), Apple finally found that there are too many undocumented programs in iOS development. It often causes wild pointers and Memory leakage due to poor memory release, various problems exist. So Apple added this item to the ios5 SDK to automatically reference the counter. Now, you can safely use it without having to count retain release. If you can ensure that your customers will upgrade to ios5 ...... Obviously, this is not very reliable for the moment ......

 

9.6 slightly more advanced applications:

So I just thought of a large number of generated temporary objects and the automatic release pool. For details about how to use them, refer to the Apple API, keywords: NSAID utoreleasepool.

9.7 Postscript:

All of the above are my own understanding of objective-C and my previous experience in making mistakes. Unfortunately, I did not pass the Chinese examination, so I wroteArticleI am not very familiar with it. Please forgive me. If you have any questions or comments, please also point out the discussion and study together. The instance code is very simple. If a friend does not understand it, please write your own demo for verification. For Delegation, notification, attributes, and other things, I would like to talk about them here. But because of the large jump in thinking, I still need to write them out again.

 

 

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.