Objective-C memory management-ARC, objective-c_arc

Source: Internet
Author: User

Objective-C memory management-ARC, objective-c_arc
ARC of memory management and automatic release pool I. Variable ownership modifier in ARC

Variable modifiers are used to identify the lifecycle of objects. They are not used in manual memory management.
Variable ownership modifiers in the ARC environment mainly include the following:

__strong   __weak__unsa_unretained__autoreleasing

Detailed description:
(1) The default values of all variables are_ StrongModify
As long as a strong reference exists, the object cannot be released. When the scope of the object is exceeded and there is no strong reference, the object will be automatically destroyed.
_ StrongThe attribute can basically adapt to all the conditions in the ARC environment. If not written, the default value is_ StrongAttribute.
(2)_ WeakNot holding objects, just simple reference.
That is to say,_ WeakObject destruction is not affected, as long_ WeakIf the modified object does not have a strong reference, it will be automatically destroyed._ WeakThe variable is automatically set to nil.
The following is an example.

  NSString *__weak str = [][NSString alloc] initWithFormat:@"I am studying"];  NSLog (@"str : %@",str);

Because the str object is a weak reference, the compiler will prompt that this is a weak reference and the output result is str: null.

When you open the ARC, you cannot use the retainrelease autorelease operation. The code that needs to be manually added to handle the reference count of memory management can be automatically completed by the compiler, however, you need to use weak and strong on the object attributes, where strong is equivalent to the retain attribute, while weak is equivalent to assign, and the basic type is still assign.

Ii. automatically release the pool
  • The Auto Release pool is a collection of objects that can be automatically released.
  • (Id) autorelease; // the method provided by NSObject. This method sends a release message to an object at a scheduled time. The returned value is the object that receives the message. in fact, when sending an autorelease message to an object, the object is added to the Auto Release pool. When the automatic release pool is destroyed, the release message is sent to all objects in the pool.
Temporary object and owning object

When using methods such as arrayWithCapacity to obtain temporary objects, you do not have to consider the issue of memory release.
[NSColor blueColor]; a singleton object will never be destroyed, but you do not have to consider its memory problems.
If your custom class depends on other objects, You need to override the dealloc method and release the dependent objects in this method.
If the designed cyclic body occupies a large amount of memory, we recommend that you manually create an automatic release pool. For example:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];   for ( int i = 0; i < 1000000; i++) {           id object = [someArray objectAtIndex:i];       NSString *desc = [object description];     // and do something with te descripton       if ( i % 100 == 0) {           [pool release];                            pool = [[NSAutoreleasePool alloc] init];       } }
The essence of APC
  • In essence, ARC is implemented by the compiler in the compilation phase, and the retain and release methods are inserted in appropriate places.
  • ARC is a feature during compilation.
About ROP and non-ROP

Ownership ownership

NSString * theString = @ "Hello, Henan"; CFStringRef cfString = (CFStringRef) theString // _ bridge cfString = (_ bridge CFStringRef) theString // the pointer ownership remains unchanged, therefore, theString // _ bridge_retained cfString = (_ javascfstringref) theString // the pointer ownership object is cfString // _ bridge_transfer cfString = (_ bridge_transfer CFStringRef) theString // the pointer ownership belongs to theString
Use certain mandatory provisions of ARC

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.