iOS in-memory management (ARC)

Source: Internet
Author: User

iOS memory management, I believe that everyone is not unfamiliar, before the use of MRC, by the developer manually to manage the memory, and later used arc, for the system to manage memory. This article mainly discusses the Autorelease,core Foundation object in the memory management aspect to notice the place.

Autorelease

When it comes to memory management, we have to mention autorelease, although we are seldom aware of its existence in our usual development. Autorelease is the meaning of automatic release, if the variable is modified using Autorelease, it indicates that the release of the variable is done by the system.

Autoreleasepool is created by Runloop when it is opened or awakened, and when Runloop enters Sleep or is released, Autoreleasepool sends a release message to all objects in the pool. Then, a problem arises, if the runloop does not go to sleep or not released (for example: the main thread, or some resident thread), the objects inside the pool will not be freed, they will accumulate in memory, but the system will do some optimizations, as follows:

12345678 - (NSMutableArray*)createArrayNoAutorelease{    idarr = [NSMutableArrayarrayWithCapacity:3];    returnarr;}- (NSMutableArray*)createArrayAutorelease{    return[NSMutableArray arrayWithCapacity:3];}

The above two methods are all indicated to return Nsmutablearray this object, but the two kinds of writing different, the system does not have the same processing.

We first call the first method Createarraynoautorelease method, then use Xcode product->perform action->assemble xxx To see, generate the following code:

1234567891011121314151617181920212223 Lfunc_begin1:....前面省略    bl  _objc_retainAutoreleasedReturnValue    .loc    2 56 14                    str r0, [sp, #4]    .loc    2 57 12 is_stmt 1          ldr r0, [sp, #4]    bl  _objc_retain    add r1, sp, #4    movs    r2, #0    .loc    2 58 1                     str r0, [sp]                @ 4-byte Spill    mov r0, r1    mov r1, r2    bl  _objc_storeStrong    ldr r0, [sp]                @ 4-byte Reload    .loc    2 58 1 is_stmt 0           add sp, #16Ltmp3:    pop.w   {r7, lr}    b.w _objc_autoreleaseReturnValueLtmp4:Lfunc_end1:

Objc_retainautoreleasedreturnvalue and Objc_autoreleasereturnvalue are mainly used to optimize the operation of the program. The returned object should have been registered to Autoreleasepool, but with these two functions, it is possible to pass the object to the caller without registering it in Autoreleasepool, which is a measure of performance tuning.

Let's take a look at the call Createarrayautorelease method, as follows:

123456789101112131415 Lfunc_begin0:... 省略    bl  _objc_retainAutoreleasedReturnValue    add r1, sp, #4    movs    r2, #0    .loc    2 44 8                    str r0, [sp, #4]    .loc    2 49 1 is_stmt 1           mov r0, r1    mov r1, r2    bl  _objc_storeStrong    add sp, #24    pop {r7, pc}Ltmp1:Lfunc_end0:

Now the object is registered in the Autoreleasepool. We can use:

1 po [NSAutoreleasePoolshowPools]

To look at the current autoreleasepool situation, you will find an extra object, such as:

(I only intercepted a part)

Note: For alloc/new/copy/mutablecopy such as return objects, the compiler optimizes them to createarraynoautorelease the same situation

The internal structure of autoreleasepool, the principle of implementation, etc., can be see:

Http://www.cocoachina.com/ios/20150610/12093.html

Core Foundation

The Core Foundation object is a set of C-language interfaces that can be converted to and from the OC Objects of the foundation framework.

To figure out the memory management of the core Foundation object, you need to understand: __bridge, __bridge_retained, __bridge_transfer; Cfretain (), cfrelease () The concept of several key words.

Cfretain (), Cfrelease (): The Memory management of Core Foundation objects is much like the retain and release of the previous MRC era.

    {        Cfstringref str = cfstringcreatewithcstring (kcfallocatordefault, "test", kCFStringEncodingUTF8);        NSLog (@ "%@", str);        Cfrelease (str);    }

Note: the Cfrelease (str) method is called here, otherwise there will be a memory leak.

__bridge: Do only type conversions and do not modify object holders. As follows:

    {        Cfstringref str = cfstringcreatewithcstring (kcfallocatordefault, "test", kCFStringEncodingUTF8);        NSString *obj = (__bridge nsstring*) str;        NSLog (@ "%@", obj);//        cfrelease (str);    }

For converting from CF to OC object, be sure to call the Cfrelease (str) method, otherwise there will be a memory leak, because just do a simple pointer address transformation, STR is still not released.

Consider an example of a wild pointer:

    Cfmutablearrayref cfobject = NULL;    {        id obj = [[Nsmutablearray alloc] init];        Cfobject = (__bridge cfmutablearrayref) obj;        Cfshow (Cfobject);    }    Cfrelease (Cfobject);

Because __bridge does not hold the Obj object, when the curly brace ends, obj is released and Cfobject becomes a wild pointer, which causes the program to crash when the Cfrelease method is called.

__bridge_retained: Used to convert the OC object to a CF object, the holder also changes, and the Cfrelease method needs to be called.

__bridge_transfer: Converts a CF object to an OC object while giving the object ownership to arc without calling the Cfrelease method, as follows:

    {        Cfstringref str = cfstringcreatewithcstring (kcfallocatordefault, "test", kCFStringEncodingUTF8);        NSString *obj = (__bridge_transfer nsstring*) str;        NSLog (@ "%@", obj);//        cfrelease (str);    }

So when using CF objects, pay special attention to memory issues.

Reference article:

https://yq.aliyun.com/articles/58964

Https://juejin.im/entry/579bfdfe5bbb500064d18aca

iOS in-memory management (ARC)

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.