IOS memory management (ARC), ios memory management (arc)

Source: Internet
Author: User

IOS memory management (ARC), ios memory management (arc)

I believe everyone is familiar with the memory management of iOS. Previously, MRC was used, and developers manually managed the memory. Later, ARC was used to manage the memory by the system. This article focuses on Autorelease, which focuses on the memory management of Core Foundation objects.

Autorelease

When it comes to memory management, we have to mention autorelease, although we seldom perceive its existence in normal development. Autorelease means automatic release. If the variable is modified using autorelease, it indicates that the release of the variable is completed by the system.

Autoreleasepool is created when runloop is enabled or awakened. When runloop goes to sleep or is released, autoreleasepool sends a release message to all objects in the pool. Then, a problem arises. If the runloop does not enter sleep or is not released (for example, the main thread or some resident threads), the objects in the pool will not be released, they will accumulate in the memory, but the system will make some optimizations, as shown below:

- (NSMutableArray*)createArrayNoAutorelease{    id arr = [NSMutableArray arrayWithCapacity:3];    return arr;}- (NSMutableArray*)createArrayAutorelease{    return [NSMutableArray arrayWithCapacity:3];}

Both of the above methods indicate that the NSMutableArray object is to be returned, but the two methods are different, and the processing done by the system is also different.

Call the createArrayNoAutorelease method of the first method, and then use xcode's Product-> Perform Action-> Assemble xxx to generate the following code:

Lfunc_begin1 :.... bl_objc_retainAutoreleasedReturnValue.loc2 56 14 strr0, [sp, #4] is omitted. loc2 57 12 is_stmt 1 ldrr0, [sp, #4] bl_objc_retainaddr1, sp, #4movsr2, #0. loc2 58 1 strr0, [sp] @ 4-byte Spillmovr0, r1movr1, r2bl_objc_storeStrongldrr0, [sp] @ 4-byte Reload. loc2 58 1 is_stmt 0 addsp, #16Ltmp3: pop. w {r7, lr} B. w_objc_autoreleaseReturnValueLtmp4: Lfunc_end1:

Objc_retainAutoreleasedReturnValue and objc_autoreleaseReturnValue are mainly used to optimize program running. The returned object should have been registered in the autoreleasepool. But with these two functions, you can directly pass the object to the caller instead of registering it in the autoreleasepool, this is a performance tuning measure.

Let's take a look at calling the createArrayAutorelease method, as shown below:

Lfunc_begin0 :... omit bl_objc_retainAutoreleasedReturnValueaddr1, sp, #4movsr2, #0. loc2 44 8 strr0, [sp, #4]. loc2 49 1 is_stmt 1 movr0, r1movr1, r2bl_objc_storeStrongaddsp, # 24pop {r7, pc} Ltmp1: Lfunc_end0:

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

po [NSAutoreleasePool showPools]

Let's take a look at the current situation of autoreleasepool and we will find an extra object, such:

(I only intercepted some of them)

Note: For methods such as alloc/new/copy/mutableCopy as return objects, the compiler will optimize them to the same conditions as createArrayNoAutorelease.

For the internal structure and implementation principle of autoreleasepool, see:

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

Core Foundation

Core Foundation objects are a group of C language interfaces that can be converted to the OC objects of the Foundation framework.

To understand the memory management of Core Foundation objects, you need to understand the key words __bridge, _ bridge_retained, _ bridge_transfer; CFRetain (), CFRelease.

CFRetain (), CFRelease (): the memory management method of Core Foundation objects, which is similar to retain and release in the MRC era.

    {        CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, "test", kCFStringEncodingUTF8);        NSLog(@"%@", str);        CFRelease(str);    }

Note: The CFRelease (str) method must be called here, otherwise Memory leakage may occur.

_ Bridge: Only perform type conversion without modifying the object owner. As follows:

    {        CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, "test", kCFStringEncodingUTF8);        NSString *obj = (__bridge NSString*)str;        NSLog(@"%@", obj);//        CFRelease(str);    }

For converting from CF to OC object, you must call the CFRelease (str) method. Otherwise, memory leakage occurs, because str is still not released after simple pointer address conversion.

Let's look at 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 obj objects, when the braces are closed, obj is released, and cfObject becomes a wild pointer. When the CFRelease method is called, the program crashes.

_ Bridge_retained: Converts an OC object to a CF object, and the owner also changes. You need to call the CFRelease method.

_ Bridge_transfer: Convert a CF object to an OC object and assign the object ownership to the ARC. You do not need to call the CFRelease method as follows:

    {        CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, "test", kCFStringEncodingUTF8);        NSString *obj = (__bridge_transfer NSString*)str;        NSLog(@"%@", obj);//        CFRelease(str);    }

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

 

References:

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

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

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.