Dark Horse programmer-memory management-autorelease and arc Mechanism

Source: Internet
Author: User

I. autorelease

Previously we used manual release objects, but sometimes we need to delay the release objects. here we need to use autorelease. The system will put the current object in the current autorelease pool. When the autorelease pool is destroyed, it performs a release operation on all objects in the current pool. For each runloop, the system will implicitly create an autorelease pool. These pools exist in the form of stack structures. At the end of each runloop, the pools at the top of the current stack will be destroyed, all objects perform a release operation.

1. Basic usage of autorelease
1> objects are placed in an automatic release pool.
2> when the automatic release pool is destroyed, all objects in the pool are release once.
3> returns the object itself.
4> after the autorelease method is called, the counter of the object remains unchanged.

2. Advantages of autorelease
1> no longer care about the time when the object is released
2> no need to worry about when to call release

3. Notes for using autorelease
1> do not use autorelease for objects that occupy a large amount of memory.
2> the use of autorelease for objects with small memory usage has no significant impact.


4. incorrect syntax
1> after alloc, autorelease is called and release is called.
@ Autoreleasepool
{
// 1
Person * P = [[person alloc] init] autorelease];

// 0
[P release];
}

2> multiple consecutive autorelease calls
@ Autoreleasepool
{
Person * P = [[[person alloc] init] autorelease];
}

5. automatically release the pool
1> when the IOS program is running, countless pools are created. These pools all exist in the stack structure (Advanced and later)
2> when an object calls the autorelease method, it is placed in the release pool at the top of the stack.


6. automatic release of the pool Creation Method
1> before IOS 5.0
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];

[Pool release]; // [pool drain];

2> starting from IOS 5.0
@ Autoreleasepool
{

}
The following is the sample code:

 1 #import <Foundation/Foundation.h> 2  3 @interface Person : NSObject 4 @property (nonatomic, assign) int age; 5  6 + (id)person; 7  8 + (id)personWithAge:(int)age; 9 10 @end
1 # import "person. H "2 3 @ implementation person 4 5 + (ID) person 6 {7 return [[[self alloc] init] autorelease]; 8} 9 10 + (ID) personwithage :( INT) age11 {12 person * P = [self person]; 13 p. age = age; 14 return P; 15} 16 17-(void) dealloc6 {19 nslog (@ "% d-year-old person destroyed", _ age ); 20 21 [Super dealloc]; 22} 23 @ end
1 #import "Person.h"2 3 @interface GoodPerson : Person4 5 @property (nonatomic, assign) int money;6 7 @end
#import "GoodPerson.h"@implementation GoodPerson@end
 1 #import <Foundation/Foundation.h> 2 #import "Person.h" 3 #import "GoodPerson.h" 4  5 int main() 6 { 7     @autoreleasepool { 8         Person *p = [Person personWithAge:100]; 9         10         11         GoodPerson *p2 = [GoodPerson personWithAge:10];12         13         p2.money = 100;14     }15     return 0;16 }

Note:

1. The methods provided by the system do not contain alloc, new, or copy. The returned objects are all autorelisted, for example, the following two examples.

Nsstring * STR = @ "123123 ";
Nsstring * str2 = [nsstring stringwithformat: @ "Age is % d", 10];


The following calls the object method and requires the release operation.
Nsnumber * num = [[nsnumber alloc] initwithint: 10];

[Num release];

2. Some class methods are often provided in development to quickly create an object that has been autorelisted
1> do not directly use the class name when creating an object. Generally, use self
+ (ID) person
{
Return [[self alloc] init] autorelease];
}

Ii. Arc Mechanism

The arc mechanism is introduced by ios5. Its full name is "automatic reference counting", which is called "automatic reference counting. With Arc, we do not need to manually operate the reference count of memory management, such as retain, relesase, and autorelease keywords. The whole process of arc helps us manage the memory, saving a lot of code and having to worry about memory leakage.

Features of arc:

1. Arc features
1> release, retain, retaincount, and autorelease cannot be called.
2> dealloc can be rewritten, but [Super dealloc] cannot be called.
3> @ property parameters
* Strong: The member variable is a strong pointer (applicable to the OC object type)
* Weak: The member variable is a weak pointer (applicable to OC object types)
* Assign: applicable to non-OC Object Types
4> change the previous retain to strong.

2. Arc Judgment Principle: as long as there is no strong pointer to the object, the object will be released.

The following code is the difference between not using arc and using arc.

Create a person class.

Non-Arc

 1 #import <Foundation/Foundation.h> 2  3 @class Dog; 4  5 @interface Person : NSObject 6  7 @property (nonatomic, retain) Dog *dog; 8  9 @property (nonatomic, retain) NSString *name;10 11 @property (nonatomic, assign) int age;12 13 @end
 1 #import "Person.h" 2  3 @implementation Person 4  5 - (void)dealloc 6 { 7     NSLog(@"Person is dealloc"); 8  9     [_dog release];10     [_name release];11     12     [super dealloc];13 }14 15 @end
 1 #import <Foundation/Foundation.h> 2 #import "Person.h" 3 #import "Dog.h" 4  5 int main() 6 { 7     Dog *d = [[Dog alloc] init]; 8     Person *p = [[Person alloc] init]; 9     p.dog = d;10     [p release];11     [d release];12     13     return 0;14 }

 

After using arc, the Code becomes as follows:

 1 #import <Foundation/Foundation.h> 2  3 @class Dog; 4  5 @interface Person : NSObject 6  7 @property (nonatomic, strong) Dog *dog; 8  9 @property (nonatomic, strong) NSString *name;10 11 @property (nonatomic, assign) int age;12 13 @end
1 # import "person. H "2 3 @ implementation person 4 5 // If You Want To rewrite dealloc, disable the last [Super dealloc]; 6-(void) dealloc 7 {8 nslog (@ "person is dealloc"); 9 10 // [Super dealloc]; 11} 12 13 @ end
 1 #import <Foundation/Foundation.h> 2 #import "Person.h" 3 #import "Dog.h" 4  5 int main() 6 { 7     Dog *d = [[Dog alloc] init]; 8     Person *p = [[Person alloc] init]; 9     p.dog = d;10 11     return 0;12 }

Obviously, after using the arc mechanism, it is forbidden to call the release, retain, retaincount, and autorelease keywords. The original retain is replaced by strong, and the call of [Super dealloc] is forbidden when the dealloc method is rewritten.

 

Dark Horse programmer-memory management-autorelease and arc Mechanism

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.