iOS Development Learning Note 013-memory management

Source: Internet
Author: User

Memory Management1. Basic Knowledge2. Close the arc mechanism3. @property4. Circular reference   5, automatic release of the pool

first, the basic knowledge

Classification of memory

Stacks: Local Variables

Heap: Dynamic application of objects, variables, etc.

Global (Static): static variable, const variable, global variable, etc.

Reference counter

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

4 bytes of storage space within each OC object to store reference counters

Role

When you create a new object using Alloc, new, or copy, the reference counter for the object defaults to 1.

When an object has a reference counter value of 0 o'clock, the memory consumed by the object is reclaimed by the system. In other words, if the object's counter is not 0, then during the entire program run,

The memory it occupies cannot be recycled unless the entire program has exited.

Actions that reference counters

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

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

3, can send Retaincount message to the object to get the current reference counter value

Summary: The beginning and ends, there are plus and minus.

second, close the arc mechanism

If you want to call the release function yourself, you need to turn off the ARC function and close the method reference

Http://jingyan.baidu.com/article/358570f67babbcce4724fcd8.html

Management Mode set method plus 1,dealloc minus 1

1, want to use an object, you should let the object's counter plus 1 (retain)

2, when you do not want to use an object, you should let the object's counter minus 1 (release)

2, who retain who release, who Alloc who release

Memory Management Specification:

1, as long as the call Alloc must have release, if not alloc it does not need to release

2. Set method

Basic data type directly assigned value

OC Object type.

First, it is not the same object if (car! = _car).

Then the old object release, the new car to do a retain operation.

3, Dealloc

Be sure to call [Super Dealloc] and put it on the last side

Be sure to release the current object once

third,@property

@property//default is assignment, retain parameter for memory management

int // The default value is assignment // retain parameters for memory management

Memory Management Summary:

1, memory management of the relevant parameters

Retain:release old value, retain new value

Assign: Direct assignment, which is the default, for non-OC object types

Copy:release old value, copy new value

2. Whether to generate a set method

@property (readonly) int age; Read only, generate Getter methods only

@property (readwrite) int name; Read/write, default is read/write

3. Multithreading Management

Nonautomic: High Performance,

Automic: Low performance (default)

@property (nonautomic, assign) int age; Write it later.

4. Name of setter and getter method

@property (setter = myAge:) int age; Setter = Set method name, custom setter method name, do not forget colon

@property (getter = getage) int age; Getter = Get method Name, custom getter method name

@property (getter = Isrich) BOOL rich; Typically this is used in variable declarations of type bool, with the getter method name starting with the IS

Iv. Circular References

Class A refers to Class B, and Class B refers to Class A.

Workaround: @class a;//Just tell compiler A is a class

Before the class reference, use the keyword @class A in the declaration file;

difference between @class and #import

1, #import方式会包含被引用类的所有信息, including the variables and methods of the referenced class; @class just tell the compiler in the A.h file that B *b is just a class declaration, what information is in this class, there is no need to know, and so on when the implementation of the file is really used, To actually see the information in Class B.

2, if there are hundreds of header files are #import the same file, or these files are #improt, then once the initial header file changes slightly, the subsequent reference to this file all the classes need to be recompiled again, such efficiency is conceivable, and relatively speaking, This is not the case with the @class method.

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 use the #import method to introduce the referenced class

referencing a specification of a class in development

1. Declare the class with @class in the. h file

2, in the. m file with #import to contain all the things of the class

3. Circulating Retain solution

One end with retain, one end with assign

v. Automatic release of the pool

Autorelease Basic Usage

1, the object will be placed in an automatic release pool

2, when the object release pool is destroyed, it will automatically release all objects inside (release operation)

3, the object itself is returned

4, after calling Autorelease, the object's counter is unchanged

Benefits

1, no longer concerned about the time the object was released

2, no longer care about when to call release

Precautions

1, occupy large memory objects do not use Autorelease

2, the use of small memory consumption autorelease, not much impact

3, Autorelease and release, as well as alloc/new/copy one by one corresponding

1@autoreleasepool//Auto Free Pool2     {3Person *p = [[[Person alloc] init] autorelease];//The Autorelaese method returns the object itself, and the counter does not immediately change4Card *c =[[[[Card alloc] init] autorelease];5         6P.card =C;7C.person =p;8         9@autoreleasepool//can be nestedTen         { One              APerson *p =[[Person alloc] init] autorelease]; -Card *c =[[[[Card alloc] init] autorelease]; -              the              -P.card =C; -C.person =p; -              +              -         } +          A}

iOS Development Learning Note 013-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.