iOS memory management

Source: Internet
Author: User

Memory management is to ensure that the open heap space is properly released. If the heap space is not released, called "Memory Leak" uses the freed space, called "early release" to repeatedly release the same space, called "Duplicate release"

(1) When we want to release a heap, first make sure that the pointer to use this heap is complete. Avoid early release.

(2) Releasing the heap space pointed to by the pointer, first to determine which pointers point to the same heap, these pointers, can only release one. Avoid repeated releases.

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

(4) Multi-threaded operation, unable to determine which thread was last used.

"Conclusion" OC memory management, is to write the program, to ensure that the value of the counter and use the same number of object pointers, to ensure that the count increases and decreases the same number of times.

"Manual Memory Management MRC"

I. Close ARC

Project--targets-> buildsetting-> search "Gar"

"YES" and "NO"

NSObject:

-(ID) retain;//reference count plus 1

-(void) release;//reference count minus 1 theoretically reduced to 0 release, in principle a pointer can only send the message once

-(Nsuinteger) Retaincount;

Returns the current reference count of an object

Δ Two. The rules of memory management

1. If you use Alloc,retain,new (or a method that starts with new), copy (or the method that starts with copy), Mutablecopy (or the method that starts with mutablecopy), "create" the object, Must be "released" using the release or Autorelease method

Δ2. Who created who was released (which class was created, which class was released; who wrote Alloc, who wrote release).

"Precautions"

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

2. Note that the transfer of the pointer frees the old object to preserve the new object.

3. Remove the object address from an array of data structures and, if it takes a long time, should be retain.

"Related Methods"

Copy and mutablecopy: Only used to copy strings

New

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

Three. Automatic release of the pool

@autoreleasepool {

@autoreleasepool {

}

}

NSAutoreleasePool

The auto-release pool was originally an object, in order to match the arc into a keyword.

-(ID) autorelease;

Automatic release/Delayed release

Note: The auto-free pool is like an array that is deferred and does not immediately subtract 1 from the counter, but instead puts the current object into the nearest auto-release pool. When the pool is freed, the elements in each pool are freed once.

Note: In an iOS program, each trigger cycle creates and destroys an auto-free pool.

"Automatic release principle"

1. The local variables of the method can be used for automatic deallocation.

2. Non-use automatic release is not available

"Conclusion" there is only one object used in a function, which can be created using the class method.

Four. Related tools

product-> profile-> Leaks

"Automatic Memory Management arc"

From XCODE5, the default automatic memory management

automatic reference counting;

Auto reference count

The simple point is to let the compiler complete the heap space reference count plus minus, automatically released. Programmers no longer write retain release methods

Automatic memory management for "another" OC, unlike Java garbage collection. Instead, when preprocessing, add retain directly where it should be, add release where it should be released. is to add code directly.

"Another" from efficiency, ARC outperforms manual memory management.

I. Limitations of ARC

1. Using arc may cause memory to be released prematurely due to code that is not canonical.

Especially when using the Avaudioplayer class, it is likely to cause early release.

2. Import some third-party libraries, or import old code that does not support arc.

Two. Solving the arc's limitations

1. Turn code that does not use arc into an arc code

Edit-> refactor-> Convert to ARC

2.ARC Non-Arc mixed

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

Build Phase-----> Complie Source

-fno-objc-arc

Three. Tips for using arc

1. Four keyword modifier references

The __strong (strong reference) default property, its decorated object pointer, points to which object, retain the object, and the object to which it is being left.

__weak (weak reference) its decorated object pointer, pointing to any object is not retain. The object pointed to by such a pointer may disappear at any time. If the object disappears, the pointer will automatically become nil.

In iOS programming, proxy objects use weak references.

__unsafe_unretained The object pointer it modifies, pointing to any object that is not retain. When the pointing object disappears, the pointer does not become nil and still points to the object that has been disposed

__autoreleasing is used only to modify pointers that require an incoming address. Such as:

__autoreleasing Nserror * ERROR; &error;

2. Attribute () parameter, in principle, can not write retain copy, can only write strong, if do not want to retain, write weak

"Note" But in fact arc is very loose on this aspect, thanks retain also does not matter.

3.id points to an object and cannot point to the object with void * p.

int A;

void * p = &a;

ID p = [[NSObject alloc] init];

In a 4.C struct, you cannot declare an object pointer. Otherwise this pointer will not be memory managed

struct sct{

ID obj;

};

5. Dealloc of the parent class cannot be called manually (explicitly)

-(void) dealloc

{

Self.name = nil;

Automatically call the dealloc of the parent class

}

iOS 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.