IOS memory management and ios Memory Management Mechanism

Source: Internet
Author: User

IOS memory management and ios Memory Management Mechanism

Memory Management ensures that the opened heap space is correctly released. If the heap space is not released, the heap space is called [Memory Leak] and the released space is called [release in advance]

(1) When we want to release a heap, we must first determine that all the pointers using this heap have been accessed. Avoid release in advance.

(2) release the heap space pointed to by the pointer. First, determine which pointers point to the same heap. Only one of these pointers can be released. Avoid repeated release.

(3) Modular operation, which module is responsible for release will become a huge challenge.

(4) multi-threaded operations cannot determine which thread is used up.

[Conclusion] OC Memory Management ensures that the counter value is the same as the number of object pointers when writing a program, and that the Count increase and decrease are the same.

Manual memory management MRC]

1. Disable ARC

// Project-> Targets-> BuildSetting-> Search for [gar]

[YES]-> [NO]

NSObject:

-(Id) retain; // Add 1 to the reference count

-(Void) release; // The reference count minus 1 is theoretically reduced to 0 for release. In principle, a pointer can only send the message once.

-(NSUInteger) retainCount;

// Returns the current reference count of the object.

△Ii. Memory Management Rules

1. all methods starting with alloc, retain, new (or new), copy (or method starting with copy), mutableCopy (or method starting with mutableCopy) you must use the release or autorelease method to [release] All created objects]

2. Who created and released (which class was created and which class was released; who wrote alloc and who wrote release ).

[Note]

1. The member variables of the object are created in the constructor and should be released in the destructor.

2. Pay attention to the pointer transfer to release the old object and retain the new object.

3. Retrieve the object address from the data structure, such as the array. If you need to use the address for a long time, retain should be taken.

[Related methods]

Copy and mutableCopy: used to copy strings only

New

[Dog new] <=> [[Dog alloc] init];

3. automatically release the pool

@ Autoreleasepool {

@ Autoreleasepool {

}

}

NSAID utoreleasepool

// The automatic release pool is originally an object and is changed to a keyword in combination with ARC.

-(Id) autorelease;

// Automatic release/delayed release

[Note] the automatic release pool is similar to an array for delayed release. Instead of reducing the counter by 1 immediately, the current object is placed in the nearest automatic release pool. When the pool is released, the elements in each pool are released once.

[Note] in iOS, an automatic release pool is created and destroyed for each trigger cycle.

[Automatic release principle]

1. The local variables of the method can be automatically released.

2. events that are not automatically released

[Conclusion] only the objects used in a function can be created using the class method.

4. Related tools

Product> profile> leaks

[Automatic Memory Management ARC]

// Automatically manages memory after Xcode5

Automatic reference counting;

// Automatic reference count

[Note] To put it simply, let the compiler add or subtract the reference count of the heap space and release it automatically. Programmers no longer write retain release or other methods

[In addition] Automatic Memory Management of OC is different from JAVA garbage collection. Instead, add a retain directly at the place where the record should be retained during preprocessing, and add a release at the place where the record should be released. Is to add Code directly.

[Other] in terms of efficiency, ARC is superior to manual memory management.

I. Limitations of ARC

1. Using ARC may cause memory to be released in advance due to code irregularities.

// Especially when AVAudioplayer class is used, it is likely to be released in advance.

2. import some third-party libraries or import old Code, which does not support ARC.

Ii. Solve the limitations of ARC

1. Convert codes that do not use ARC into ARC Codes

Edit-> Refactor-> Convert to ARC

2. ARC non-ARC hybrid editing

// In the same project, some files use ARC, and some files do not use ARC.

Build phase -----> Complie Source

-Fno-objc-arc

Iii. techniques for using ARC

1. Four keywords modify reference

_ Strong (strong reference) default attribute, its modified Object Pointer, pointing to which object, will retain the object, leaving which object, will release the object.

_ Weak (weak reference) Its modifier Object Pointer, pointing to any object will not retain. Objects pointed to by such pointers may disappear at any time. If the object disappears, the pointer will automatically become nil.

// In iOS programming, the proxy object uses weak references.

_ Unsafe_unretained: the object pointer it modifies. It points to no retain for any object. When the pointed object disappears, the pointer will not change to nil, but will still point to the released object.

_ Autoreleasing is only used to modify the pointer of the address to be passed in. For example:

_ Autoreleasing NSError * error; & error;

2. The () parameter of the attribute. In principle, you cannot write retain copy. You can only write Strong. If you do not want retain, write Weak.

[Note] In fact, ARC is very loose in this regard. Thanks to retain, it doesn't matter.

3. The id points to the object, and the void * p cannot point to the object.

Int;

Void * p = &;

Id p = [[NSObject alloc] init];

4. The object pointer cannot be declared in the struct of C. Otherwise, the pointer will not perform memory management.

Struct sct {

Id obj;

};

5. You cannot (explicitly) manually call the dealloc of the parent class.

-(Void) dealloc

{

Self. name = nil;

// Automatically call the dealloc of the parent class

}

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.