OC for iOS Development (11)--Memory management MRC

Source: Internet
Author: User

Master Content

> Understanding the principles of memory management

> Master Manual memory management MRC

> Mastering the use of memory management in real projects

( i)

1. Understanding part

1.1 Memory Management (traditional memory management demo)

1.1.1 Memory management did a thing?

Memory management is to ensure that the open heap space is properly released

If the heap space is not released, it is called a memory leak

use freed heap space, called early release

repeatedly releasing the same space, called repeating release

1.1.2 The dilemma of traditional memory management

(1) when we want to release a heap, first determine the use of this heap of pointers, have access to complete, to avoid early release.

(2) Releasing the heap space pointed to by the pointer, first to determine those pointers to the same heap, these pointers, can only be freed once, to avoid repeated release.

(3) Modular operation, the module is responsible for release, will become the biggest problem

(4) Multi-threaded operation, it is not determined that the thread end use finished

The solution to the dilemma of 1.1.3

"Reference counter"

Reference count abbreviation counter , a pointer to an object, reference count plus 1, reduce a pointer to the object, reference count minus 1, when the reference count is 0, the system automatically frees the space occupied by the object (simulation multiplayer network game, bucket landlord, QQ Hall)

A new object is created, and the system automatically adds a reference count (Retaincount) property to the object

1.1.4 The golden Rule of memory management

1, usually with alloc,retain,new (or a method starting with new), copy (or the method at the beginning of the copy), Objects created by mutablecopy (or mutablecopy) need to be released with release or Autorelease.

2, who created who was released ( which class was created, which class was released, who wrote Alloc, who wrote release)

1.1.5 How to change the project to MRC

1 Click Project-"Build settings-> automatic Re: Search by GAR keyword. Cou.

, set the setting of this option to No

1.2.1 Alloc and release

Alloc The process of creating an object, the object reference count is 0 plus 1, which becomes 1

retain the object reference counter plus 1

release will reduce the number of object references by 1

1.2.2 Destruction Function

-(void) Considerations for the Dealloc method

Dealloc, called destructors , call order and constructors, in contrast, typically do some free memory work in the function

is called when the reference count is reduced to 0 o'clock (can be used to verify that the object is freed)

Grammar:

-(void) dealloc{    //  Note the internal code sequence //1. All member objects are called once release/ /[Super Dealloc] to write to Dealloc from the back line, [Super Dealloc] is really releasing the current object     [Super Dealloc];}

    

Use in composite of the 1.2.3 class

Retain and assign (Compiler Attribute)

retain expand

-(void) Setdog: (Dog *) dog{    if (_dog!=dog) {        [_dog release];        _dog=[dog retain];    }}

Assign expand

-(void) Setdog: (dog *) dog{    _dog=Dog;}

1.2.3 autorelease and Autoreleasepool

Autoreleasepool automatically frees the pool and manages the objects in the pool

Autorelease, when sending an autorelease message to an object, the object is stored in Autoreleasepool, and the auto- release pool automatically empties the objects inside the pool when the system finishes executing the auto-release pools.

"Note" The auto-release pool is similar to an array for deferred release, and instead of a counter minus one immediately, the current object is placed in the nearest auto-release pool, releasing every element of the pool once it is released

+ method of memory management

+ (student*) create{    *student= [[[Student alloc]init]autorelease]; //     [student autorelease];    return student;}

"Precautions"

1, the member variables of the object are created in the constructor method and should be released in the destructor

2, note the pointer transfer, release the old object, keep the new object

3, read the object address from an array in the data structure, if it takes a long time to use should retain

1.2.4 String Memory Management

1.2.4.1 Memory management of strings

1>@ "" creates a string in a read-only data segment, is read-only, and the reference count is-1, the default is that the Autorelease object cannot send it a retaincount++ or Retaincount--The message

2>alloc retain copy mutablecopy new created string requires release

3> class method creates a string and directly assigns a string, does not require manual management of memory

1.2.4.2 copy and Mutablecopy(only used to copy strings)

1>Copy message, mutable and immutable strings are converted to immutable strings

Nsmutablestring *mulstr=[[nsmutablestring alloc]initwithstring:@ "ABC"];         *str=@ "a";         *mulstr2=[mulstr copy];        [MulStr2 appendString: @" AA "];

2>mutablecopy, whether mutable or immutable, converts a mutable string

 

Nsmutablestring *mulstring=[[nsmutablestring alloc]initwithstring:@ "ABC"];         *str=@ "ab";        Nsmutablestring* mulstr=  [str mutablecopy];         [Mulstring appendString: @" AB "];

memory management for 1.2.5 arrays

1> in a mutable array, by adding and deleting objects, you can add 1 and subtract 1 to the object's reference count

2> when initialized in an immutable array, the reference count of the initialized object is added 1, and the array will decrement the reference count of the initialized object by 1 when it is destroyed (release) .

OC for iOS Development (11)--Memory management MRC

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.