OC section Seventh-memory management

Source: Internet
Author: User

Jesting:

iOS development has come to an arc era, generally do not need us to pay too much attention to how the memory is allocated, how to manage. For a long time, I do not know what memory management is a ghost, but if you encounter this problem, but can not find a solution is really a headache. So, or calm down to the heart, study well, after all, the internal strength is the level of reflection.

1. Reference counting
For a dynamic application of memory, there is a person (pointer) to use, give this memory counter plus 1, after the use is completed, the counter will be reduced by 1, when the memory reference count is 0, we release him again, so that the above problem solved. OC, which is the use of reference counting in this way to manage memory.

Vivid understanding is: to give an example of life, we go to the reservoir side barbecue, I lit the fire, when we are "in dire straits" Roast meat, a telephone came, daughter-in-law let me go home. So, I tell you, you know, brother to go back, in order to prevent fire, I will put out the fire. I believe that at this time you kill my heart all have.
Can we think of a better way, I don't pour out the fire, and not cause a fire?
Count, though the fire was born to me, but we had 10 people to use together. So, this heap of fire count is 10, if I, or any one of the classmates go, let Count minus 1, become 9,8,7 ...            In this way, until the last classmate to go, he went before the fire out of the good. This is the memory management method of OC, reference count.

2. The golden rule of memory management
For reference counting, there is a golden rule for memory management:
The basic rule to apply is everything this increases the reference counter with Alloc, [Mutable]copy[withzone:] or retain Is in charge of the corresponding [Auto]release.
As long as the alloc/retain/copy/mutablecopy is used, the object is created
Then you have to release it using release,
——— who created, who was responsible for releasing

3.retain and Retaincount
Retain, the object input retention operation, which is to make the object's reference count plus 1.
Retaincount, prints a reference count for an object.

        Dsdog*dog=[[dsdog Alloc]init];        NSLog (@ "%lu", [Dog Retaincount]),//1 contains init, reference count plus 1        dsdog *dog2=[dog retain];                NSLog (@ "%lu", [dog2 Retaincount]),//2  object retain, reference count plus 1        [dog2 release];        NSLog (@ "%lu", [dog2 Retaincount]);//1  release, reference count minus 1        [dog release];   becomes 0, destroys the object

4.MRC and Arc
ARC (Automatic Reference counting), automatic reference counting, by Xcode, helps us to manage memory.
MRC (Manual Reference counting), manual reference counting, we manage memory manually.

Version after Xcode 5.0 is ARC mode by default

The project is created with arc, and if we want MRC, we need to set up the following.
Select Project-Target-bulid Settings-search: Automatic reference counting or auto, change objective-c Automatic reference counting to No.

[Email protected] retain,assign,copy expand

[Email protected] (Nonatomic, retain) Dsdog *dog; Used to decorate OBJC objects.

The following will be expanded:
-(void) Setdog: (Dsdog *) dog
{
if (_dog! = dog)
{
[_dog release];
_dog = [dog retain];
}
}
5.2 Assign expansion: simple data type

@property (nonatomic, assign) Dsdog *dog;

Assign is copied directly, the following will be expanded:
Simple data type, OC memory management for simple data type int\float ..., OC invalid
-(void) Setdog: (Dsdog *) dog
{
_dog = dog;
}

5.3 Copy Expand, copy the original object

Copy many for string, Dict, Array
@property (nonatomic, copy) NSString *name;
-(void) SetName: (NSString *) name
{
if (_name! = name)
{
[_name release];
_name = [name copy];
}
}
6. Automatic release of the pool

Principle: When an object receives a autorelease message, it is added to the current auto-release pool, and when the auto-free pool is destroyed, the release message is sent to all objects in the pool.

OBJC provides two ways to create an auto-free pool:
Method One: Use NSAutoreleasePool to create

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]init];     //这里写代码     [pool release];
方法二:使用@autoreleasepool创建
  @autoreleasepool {     //这里写代码     }


7.weak:ARC新引入修饰词,可代替assign,比assign多增加一个特性
Strong:arc Newly introduced modifier, can replace retain
 

OC section Seventh-memory management

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.