06-Memory Management

Source: Internet
Author: User

First, the understanding of ARC

1. Introduction

* ARC is a new feature added since IOS 5, completely eliminates the cumbersome manual management of memory, the compiler will automatically insert the appropriate retain,release, The Autorelease statement. You no longer need to worry about memory management , because the compiler handles everything for you

* ARC is a compiler feature, not an IOS runtime feature, nor is it similar to a garbage collector in other languages. So ARC and manual memory management performance is the same, and sometimes faster, because the compiler can also perform some optimizations

2. Fundamentals

1> rules

the rules of ARC are very simple: as long as there is a strong pointer variable pointing to the object, the object remains in memory

2> strong pointers and weak pointers

* default All instance variables and local variables are strong pointers

* The weak pointer automatically becomes a nil pointer when the object pointed by the weak pointer is recycled, and does not throw a wild pointer error

3. Use note

* cannot call release,retain,autorelease,retaincount

* can rewrite dealloc, but cannot call [Super Dealloc]

* @property : want to have an object for a long time, should use strong, other objects with weak

* other basic data types still use assign

* both ends are referenced with strong, one end with weak

2 types of pointers

1> strong pointer: By default, all pointers are strong pointers __strong

2> weak hands:__weak

second, the basic principle

1. What is memory management

* mobile device memory is extremely limited, each app can occupy a limited amount of memory

* when the app consumes more memory, the system will issue a memory warning, then you have to reclaim some memory space that no longer needs to be used. such as retrieving some objects, variables, etc. that you do not need to use

* Management scope: Any object that inherits NSObject, for other basic data types (int,char,float,double, struct,enum, etc.) Invalid

2. Basic structure of the object

* each OC object has its own reference counter, which is an integer representing "the number of times the object has been referenced", that is, how many people are using the OC object

3. The role of reference counters

* when a new object is created using Alloc, new, or copy , the reference counter for the object is 1 by default

* when the reference counter value of an object is 0 , the memory consumed by the object is reclaimed by the system.

4. actions that reference counters

* send an retain message to the object, you can use the reference counter value +1 (theretain method returns the object itself)

* send a release message to the object , you can make reference counter value -1

* can send an retaincount message to the object to get the current reference counter value

5. Destruction of Objects

* when the reference counter value of an object is 0 , then it will be destroyed and the memory occupied by the system is recycled

* When an object is destroyed, the system automatically sends an dealloc message to the object

* generally overrides the Dealloc method, where the associated resources are released,dealloc like the object's last words

* Once the Dealloc method has been rewritten , [Super Dealloc] must be called and placed on the last side of the call

* do not call the dealloc method directly

Once the object is recycled, the memory it occupies is not available, and sticking to it will cause the program to crash (wild pointer error)

iii. settings for Xcode

1. Cancel the ARC

To manually invoke retain, Releasee, and other methods

2. turn on Zombie object monitoring

By default,Xcode does not control zombie objects, and using a piece of freed memory does not give an error. Easy commissioning, where zombie object monitoring is turned on

Iv. principles of memory management

1. Basic use of methods

1> Retain : counter +1Returns the object itself

2> Release : counter -1, no return value

3> Retaincount : Get the current counter

4> Dealloc

* when an object is to be recycled, it will be called

* Be sure to call [Super Dealloc], this call to be placed on the last side

2. Concept

1> Zombie Objects : Objects that occupy memory have been reclaimed, zombie objects can no longer be used

2> wild hands : Pointer to zombie object (memory not available), send message to wild pointer error (exc_bad_access)

3> null pointer : no pointer to anything (the stored thing is nil,NULL,0), sending a message to a null pointer does not give an error

3. Summary

* you want to use (occupy) an object, you should let the object's counter +1(Let the object do a retain operation)

* you don't want to use (occupy) an object, you should let the object's counter -1(Let the object do a release)

* who retain, who release

* who alloc, who release

v. Memory management of the Set method

If you have a member variable of OC object type, you must manage the memory of this member variable. For example, there is a book *_book;

1. Implementation of Set method

-(void) Setbook: (Book *) book{    if (book! = _book) {        [_book release]        ; = [book retain];    }}

2. Implementation of the Dealloc method

-(void) dealloc {    [_book release];    [Super Dealloc];}
vi..@property parameters

1. controlling The memory management of the Set method

* Retain : release old value,retain new value (for OC object)

* Assign : Direct Assignment, no memory management ( default, for non- OC Object Types )

* Copy : release old value,copy new value (generally used for NSString *)

2. control requires no need to generate set method

* ReadWrite: Generate both the Set method and the get method (default)

* ReadOnly: Only the get method is generated

3. Multithreading Management

* Atomic: Low performance (default)

* Nonatomic: High Performance

4. control The name of the set method and the Get method

* Setter: Set The name of the set method, there must be a colon:

* Getter: Sets the name of the Getter method

Seven, circular reference

1. @class

* Usage Scenarios

For circular dependencies, such as Class a refers to Class B, and class B also applies class a

This code compilation will error. When using @class to declare each other in two classes, there is no compile error

* Usage

Use @class class name: You can refer to a class to illustrate that it is a class

* and the difference between #import

1> #import method will contain all the information of the referenced class, including the variables and methods of the referenced class,@class Way just to tell the compiler in the A.h file that B *b is simply the declaration of the class, The specific information in this class, there is no need to know, and so on when the implementation of the file is really used, will really go to see the information in class B

2> If there are hundreds of header files #import the same file, or if these files are #improt in turn, then once the initial header file is slightly changed, All of the classes that refer to this file later need to be recompiled again, so the efficiency is predictable, and the @class Way is not the case.

3> in the . M implementation file, if you need to refer to an entity variable or method of the referenced class, you also need to introduce the referenced class by using the #import method

2. cyclic retain

* For example a object retain a B object, B object retain a Object

3. Solution

when both ends are referenced by each other, the end should be retain, one end with the right IGN .

Eight,autorelease

1. autorelease

* When sending an autorelease message to an object , the object is added to an auto-release pool

* when the auto-release pool is destroyed, a release message is sent to all objects inside the pool

* when calling the Autorelease method, the object's counter is not changed and the object itself is returned

* Autorelease actually just delays the call to release , and for each autorelease, the system simply puts the object into the current autorelease pool , when the pool is released, all objects in the pool are called release

2. automatic release of pool creation

@autoreleasepool {    //  ...}

* During program run, you can create multiple auto-release pools, which are in memory in the form of stacks

* OC object only needs to send a autorelease message, this object will be added to the nearest auto-release pool

3. Application Examples

Book *book = [[[Book Alloc] init] autorelease];

* You can typically add a class method that quickly creates an object for a class

+ (ID) book {    return [[[Book alloc] init] autorelease];}

06-Memory Management

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.