Objective-C memory management,

Source: Internet
Author: User

Objective-C memory management,

Management scope: any object that inherits NSObject. Basic data types are not managed.

Essentially: Because objects and basic data types are different in the system's storage space, local variables are mainly stored in the stack, while objects are stored in the heap, when the code block ends, all the local variables involved in the code block will be recycled, and the pointer to the object will be recycled. At this time, no pointer is directed to the object, but it still exists in the memory, memory leakage.

Cause:

1. memory overflow: no pointer to the object, but the object is not recycled

2. Abnormal wild pointer: a pointer to a zombie object. A zombie object is a collection object of memory.

No error is returned when messages are sent to null pointers.

Each oc object has a dedicated 4-byte storage space to store reference counts.

 

Memory Management Method:

1. manual reference count)

2. auto reference counting)

MRC memory management mechanism: reference count

ARC is based on MRC, and ARC is a compiler feature, not a runtime feature.

Memory Management Principles

(1) Principles As long as someone else is using an object, this object will not be recycled; As long as you want to use this object, you should make the reference counter of this object + 1 When you do not want to use this object, you should make the reference counter of the object- 1 (2) who created and who release (1) If you use alloc, new To create an object, you must call the release or autorelease method. (2) You are not responsible for what you created.   (3) who retain and who release If you call retain, you must call release no matter how the object is generated. (Iv) Summary There should be a reduction in the beginning and end. Used to add an object counter 1 In the end- 1 .Memory Management for Attribute assignment: retain: Execute release on the old object first, and then assign a value to copy: execute release on the old object and copy the new object

Internal Implementation of attributes under retain

-(Void) setName :( NSString *) name {

If (_ name! = Name ){

[_ Name release];

_ Name = [name retain];

}

}

-(NSString *) name {

Return [[_ name retain] autorelease];

}

Use retain to add 1 to the reference count of the object. If the retain is not used for simple assignment, the reference count of the object will not change.

Assign, retain, and copy correspond to different setter implementations. When assigning values to instance variables, use the setter method whenever possible. When assigning values again, the previous value is release.

Dealloc is automatically called when the object reference count is 0. Do not call it explicitly.

Inside dealloc implementation, release the instance variables and then execute [super dealloc].

The memory management of the constructor is implemented by using autorelease.

The collection manages its own elements. KVC is a method for indirectly accessing instance variables. The ARC system manages memory, which does not need to be manually managed by developers.

 

When the object autorelease is added to the autoreleasepool, the object is destroyed when the autoreleasepool is destroyed.

If the built-in method does not contain alloc,new,For example, [NSDate date]; 

In development, some class methods are often written to quickly create an autorelease object. When creating an object, do not directly use the class name, but use self

Nonatomic sets the property setter. getter operations are not atomic, not thread-safe, and atomic operations are atomic.

 

In OC, copy is used to generate a copy object using a source object (Copy must follow the NSCopying Protocol)

Features: 1. Modifying the attributes and behaviors of the source object does not affect the copy object.

2. Modifying the attributes and behaviors of a copy object does not affect the source object.

Usage:

An object can call the copy or mutableCopy method to create a copy object.

1. copy: an unchangeable copy (such as NSString, NSArray, and NSDictionary ).

2. mutableCopy: a mutable copy (such as NSMutableString, NSMutableArray, and NSMutableDictionary ).

 

Prerequisites for using the copy function:

1. copy: The NSCopying protocol must be followed to implement copyWithZone: method.

@ Protocol NSCopying

-(Id) copyWithZone :( NSZone *) zone;

@ End

2. mutableCopy: The NSMutableCopying protocol must be followed to implement mutableCopyWithZone: Method

@ Protocol NSMutableCopying

-(Id) mutableCopyWithZone :( NSZone *) zone;

@ End

Only when both the source object and the copy object are unchangeable is the shortest copy. Others are all deep copies.

What is the difference between deep replication and light replication:

1. deep copy (deep copy, content copy, and deep copy ):

Features: 1. The source object and the copy object are two different objects;

2. The reference counter of the source object remains unchanged, and the counter of the copy object is 1 (because it is newly generated ).

Essence: new objects are generated.

2. shallow copy (shallow copy, pointer copy ):

Features: 1. The source object and the copy object are the same object;

2. The counter + 1 is referenced by the source object (copy object), which is equivalent to a retain operation.

Essence: no new object is generated.

 

Shallow copy: the object opens up new space. The memory address of the object is different, and the instance variable points to the same memory.

-(Id) copyWithZone :( NSZone *) zone

{

Person * p = [[Person allocWithZone: zone] init];

P. string = self. string;

Return p;

}

 

Deep copy: the memory addresses of objects and instance variables are different.

-(Id) copyWithZone :( NSZone *) zone

{

Person * p1 = [[Person allocWithZone: zone] init];

P1.string = [self mutableCopy];

Return p1;

}

 

Pseudo copy:

-(Id) copyWithZone :( NSZone *) zone

{

Return [self retain];

}

 

Summary of the features of ARC:

1) Release, retain, and retainCount cannot be called.

2) Dealloc can be rewritten, but cannot be called [super dealloc]

3@propertyParameters: Strong: equivalent to the original retain (applicable to the OC object type). The member variable is a Strong pointer. Weak: equivalent to the original assign (applicable to the oc object type). The member variable is a Weak pointer. Assign: applicable to non-OC object types (basic types)  Supplement Make the program compatible with the ARC and non-ARC sections. Transition from non-ARC-fno-objc-arc to ARC-f-objc-arc. ARC also needs to consider the circular reference problem: one end uses retain and the other end uses assign. Tip: the string is a special object, but it does not need to be manually released using release. This string object is autorelease by default, and no extra memory is needed.

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.