Arc vs MRC

Source: Internet
Author: User

I. Managing memory by reference count

1, reference count. Manage memory by reference count. When an object is created, the reference count is at least 1, the reference count is incremented by retain, the reference count is decremented by release, Autorelease, the reference count is 0 o'clock, the memory of the object is reusable, and all references to that object are invalid.

2, dangling pointer. A reference to an object that has a reference count of 0. Using dangling pointer does not necessarily cause the program to crash, whether the crash is to see if the object memory is overwritten.

3, reference tree. The root object in the iOS application is the UIApplication object.

4, attribute access. Strong relationship modified property, when SetValue, retains the new value before releasing the old value. This order cannot be reversed, and once reversed, if the new and old values are the same object, it is possible that the object has been reclaimed after release, and then there is a problem.

5, Autorease. When an object is created inside the return method, because the object cannot be disposed inside the method (the object reference count value is potentially 0, the object is recycled), the reference count value is decremented at the next event loop by Autoreleasepool. objects created through the class factory method are placed in the Autoreleasepool.

6, retain cycle. Cause a memory leak, there are two ways to solve the problem, one is weak reference, one is that the code controls one of the objects no longer retains another object.

7. Static Analyzer in the Clang compiler. You can identify memory management issues, or you can pre-join retention and release operations as needed.

Note: The Retaincount method is not very useful for getting reference count values. When you increment or decrement a reference counter, you can only say increment or decrement, and never say that the hold count must be a value.

Ii. Arc Auto Reference count (compiler and run-time components manage memory together)

1, the realization of the idea is static analyzer.

2, the code in the presence of retain, release, Autorelease, Dealloc, the compiler cannot pass. When these methods are called, the underlying C-language functions are not directly invoked through the OC's message dispatch mechanism.

3, ARC naming rules. The method name begins with Alloc, new, copy, Mutablecopy, and the object is owned by the method caller. Details are as follows:

 /*   Newperson method, the method caller holds the returned object. So inside the method, in addition to allo the reference count of +1, there is no remaining hold and release operation,  Finally, after calling the method, the object needs to be released one more time.   */ + (person *) new  person{person  *person = [[Person alloc] init];  return   person;}  /*  createperson method, the method caller does not hold the returned object, so arc increments the [person autorelease] method when the return operation occurs.  last counter balanced.      */ + (person *) createperson{    Person  *person = [[Person alloc] init];  return   person;}  

4, ARC will sometimes be a pair to remove the retention, release operation.

5. Arc contains run-time components.

If the method name does not follow the naming convention, but the return value is assigned to a strongly-referenced modifier, there is no doubt that the retain operation needs to be increased. Given the backward compatibility performance, the return statement of the Createperson method is not replaced with the [person autorelease], instead of Objc_autoreleasereturnvalue ( person) replacement.

The Objc_autoreleasereturnvalue method is implemented at the bottom. This method examines whether the retain operation is to be performed after the method returns. If you are performing a retain operation, a flag bit in the global data structure is set, and the return object in the method is not automatically freed. And when performing a retain operation, [person retain] will also be replaced with the Objc_retainautoreleasereturnvalue (person) method, checking that the flag bit has been set, then no hold operation is performed.

Sets the flag bit and checks whether the flag bit is set (compiler execution), faster than hold and release.

6. Local variables and instance variable modifiers in arc.

Strong: Retain this value, default semantics;

Weak: This value is not preserved and the value is recycled, pointing to nil;

7. The Dealloc method in arc.

1), the Dealloc method does not allow the release message to be sent, and the parent class is not allowed to send dealloc messages. ARC generates a cleanup routine with the help of objective-c++ (objective-c++ attribute: When you reclaim a Objective-c++ object, the object that is being reclaimed invokes the destructor for all C + + objects, and the compiler discovers that C + + objects exist in the object. Generates the. Cxx_destruct method), which automatically generates code in the. Cxx_destruct method, including sending a DEALLOC message to the parent class. Singleton objects are not automatically refactored .

2), add Cfrealease (Core Foundation Object), Free (via malloc () allocated memory) method, logout notification, and so on, if necessary. The Dealloc method in Arc only releases the reference and unlocks the listener .

3), release references do not include file descriptor, sockets, large chunks of memory, and so on, typically create another method to free these references, and the invocation of another method must precede dealloc execution. Reasons for not releasing these resources in Dealloc: First, the resource retention time is too long, and the Dealloc method is not necessarily executed by Dealloc release.

4), methods that perform asynchronous tasks should not be called in Dealloc, and methods that can only be executed in a normal state should not be called in Dealloc.

8. Where there may be memory leaks in the arc:

1), there are mutual strong references, resulting in the two sides can not be released (retain cycle);

2), C code object, is not released itself, such as CG (Coregraphics.frameworks) The beginning of the object.

3), GCD object.

Arc vs MRC

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.