iOS development--Ramble on memory management (i.)

Source: Internet
Author: User

1. MRC and Arc

Apple provides two memory management mechanisms: One is the MRC (manual reference count), the manual reference count, and the other is arc (auto reference count). That is, the active reference count itself.

Manual reference counting, as the name implies. Requires the program ape to actively invoke retain, release and other methods to manage memory. Instead of actively referencing the count, the compiler inserts the memory management code into its own hands at compile-time, where it needs to be manually called. This frees the program apes from the complexity of memory management and puts a lot of other effort into implementing detailed business logic. It is important to note that. ARC should be used under xcode4.2 or above version number, LLVM compiler version 3.0.


2. How to implement the reference calculation

One is the implementation of GNUstep: When an object is generated, the retained attribute is added to the object's head. Represents the reference count of the object, retained minus 1 when the retained plus 1,release method is called when the Retain method is called. When the retained count is 0 o'clock. Releases the object.

One is how Apple is implemented: the reference count table, the approximate form of the table {object A's reference count: The memory address of object A. Reference count for object B: The memory address of object B. ..., reference count of object N: Memory address of Object n}.


3. How to think about memory management

A, self-generated objects, own hold

ID obj = [[NSObject alloc] init]; At this point, the object is generated and obj holds the object.

This series of methods includes: Alloc, new, copy, and Mutablecopy

b, not their own generated objects, they can also hold

The id obj = [nsmutablearrray array];//object is generated, and obj points to the object. However, obj does not hold the object and needs to invoke [obj retain] talent to actually hold

C. release when you no longer need the objects you hold

D. Objects not owned by themselves cannot be released

Novice may be very difficult to notice and understand the second rule. How it does it, the way it is implemented is probably as follows:

-(ID) object{   //Object generation. and obj holds the object   id obj = [[NSObject alloc] init];   The acquisition object exists, but does not own the object   [obj autorelease];   return obj;}


4, about Autorelease

Autorelease uses objects that are self-active and properly freed when they are outside the established survival range, similar to their own active variables in C. Typical use methods for Autorelease are as follows:

    NSAutoreleasePool *pool = [[NSAutoreleasePool] alloc init];    ID obj = [[NSObject alloc] init];    [obj autorelease];    [Pool drain]; When the pool object is deprecated. The [obj release] method is invoked on its own initiative

PS: Most of the ideas in this article are from the book "Objective-c Advanced Programming", the right to read the summary of the individual.








iOS development--Ramble on memory management (i.)

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.