Objective-C syntax-memory management

Source: Internet
Author: User
1. Objective-C Memory Management Overview:

Objective-C needs to consider memory management by itself. This is a great challenge for new programmers who have switched from Java, C # and other languages to the Objective-C platform. Fortunately, if you are familiar with the Objective-C memory management mechanism, manual memory management is not that terrible. Objective-C memory management is between C/C ++ and Java C #. Unlike C/C ++, all the memory management needs to be done by programmers, it is not as complete as the memory Garbage Collector in Java C. (Objective-C 2.0 has a GC mechanism, but does not support IOS ). So how does he manage the memory? Managed by reference count. PS: in iOS
After 5, the Automatic Reference Counting (ARC Automatic Reference Counting) feature is added, so that programmers do not have to worry about memory management. Unlike ARC and GC, ARC is the action of the compiler. I will talk about it later. However, it is necessary to be familiar with Objective-C's memory management mechanism.

2. memory management principle:

The Objective-C memory management model is based on object ownership. If you own this object, you have the responsibility to release it. An object can have multiple owners. If the owner of this object is 0, the system automatically releases the object. There are four principles for object ownership and release:

  • You gain ownership of any object you create. (Including objects obtained by keywords such as alloc, new, and copy)
  • Get object ownership through retain
  • If you do not need an object, you must release the ownership.
  • You cannot release objects without ownership
RetainCount is the only reference for Objective-C object reference. After the release method of instance management is called, The dealloc method of the object whose attribute is reduced to 1 and to zero is automatically called for memory recycle. That is to say, we should never manually call the dealloc method of the object. Images are from the official website of apple. 3. Example: The environment creates a Person class without selecting ARC, so that the class inherits from NSObject and implements the dealloc method in the. m file:
- (void) dealloc  {      NSLog (@"dealloc called. Bye Bye.");      [super dealloc];      } 
When the reference count is 0, this method is called to prove that the object is destroyed. Create an object and print its reference count
Person * person = [[Person alloc] init]; NSLog (@ "retainCount: % d", [person retainCount]);

ObjectPersonOfRetainCount: 1

We add 1

Person * person = [[Person alloc] init]; NSLog (@ "retainCount: % d", [person retainCount]); [person retain]; NSLog (@ "retainCount: % d of object person", [person retainCount]);

Print result:

ObjectPersonOfRetainCount: 1

ObjectPersonOfRetainCount: 2

Like the legend, retainCount is added. It can be found that when the value of release is reduced to 1, it will not be reduced.

Person * person = [[Person alloc] init]; NSLog (@ "retainCount: % d", [person retainCount]); [person retain]; NSLog (@ "retainCount: % d of object person", [person retainCount]); [person release]; [person release]; NSLog (@ "retainCount of object person: % d ", [person retainCount]);

Is the retainCount output after this code is printed 0? Print result:

16:05:29. 830 ObjectiveCTest [2847: f803] retainCount: 12012-07-05 16:05:29 of the object person. 831 ObjectiveCTest [2847: f803] retainCount: 22012-07-05 16:05:29 of the object person. 831 ObjectiveCTest [2847: f803] dealloc called. bye Bye.2012-07-05 16:05:29. 832 ObjectiveCTest [2847: f803] retainCount: 1 of object person

Shocked. When the first release, the retainCount is reduced by 1, and then the release and d object are dealloc called, but the retainCount is still 1. I checked in stackoverflow.com. Someone said this retainCount is useless .... It is true that the retainCount is 1 and the object is killed. Someone said that the person
= Nil. In this case, retainCount is 0. After trying, nil's retainlCount is always 0. This assignment is meaningless.

4. NSAID utoreleasepool Automatically releases memory

It is easy to understand if it is just for creating and releasing. But what most people do not understand is that the memory is automatically released, which is hard to understand, but the principle is also very simple.
C/C ++ programming has a very important principle of Memory Management: Who creates and destroys. However, this principle cannot be followed in some cases: for example, let a common function return a memory area. The function caller must destroy the memory after the function is called. In Objective-C's automatic memory release mechanism, the user does not have to worry about it.

Let's take a look at the most classic program entry program and use automatic release.

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];int retVal = UIApplicationMain(argc, argv, nil, nil);[pool release];

We first regard the pool as a common object, first alloc, And the retainCount of the pool is 1.
In the third statement, release, retainCount is 0, and its dealloc method is automatically called. It is no different from any other common object.
Where is the magic?
The NSMutableArray contains an array to save all objects declared as autorelease. If an object is declared as autorelease, the system will add this object to this array. During the destruction, the NSAID utoreleasepool traverses the array and each member in the release array. If the retain count of the members in the array is 1 at this time, after release, the retain count is 0,

NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init]; Person * person = [[Person alloc] init]; NSLog (@ "retainCount of object person: % d ", [person retainCount]); [person autorelease]; NSLog (@ "here, person has not been released"); [pool release];

Print result:

16:34:38. 823 ObjectiveCTest [3045: f803] retainCount: 12012-07-05 16:34:38 of the object person. 823 ObjectiveCTest [3045: f803] till now, person has not been released 16:34:38. 824 ObjectiveCTest [3045: f803] dealloc called. bye.

The person is released only when pool release is called. This is the magic of automatic release, so that you can create objects.

Copyright Disclaimer: This article will be published at http://blog.csdn.net/totogo2010/, and you will be welcomed to enjoy the transfer and sharing. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank 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.