OC Memory Management: MRC vs. arc

Source: Internet
Author: User

  five large areas in memory : stack area, heap area , BBS segment , data segment and code snippet , in addition to the heap area, memory management of other areas is reclaimed by the system itself OC objects are stored in the heap , so the memory management of OC is mainly managed by "OC objects in the heap area "  several concepts in memory management:Reference Calculator : Both retaincount, there are 1 8-byte spaces inside each OC object to store Retaincount, indicating how many "people" are being used; when an object has just been created, the default count value is 1, and when the count value is 0 o'clock, the system automatically calls the Dealloc method to destroy the objectReference counter Usage: send an appropriate technical action to the object to change the value of the counterretain message: Make counter +1Release message: Make Counter-1Retaincount message: Gets the current value of the current Retaincount---Wild pointer: pointer variable without initializationZombie object: Refers to an object that has been recycled, but whose data is still in memoryZombie objects may or may not be accessible, depending on whether the occupied space has been reassigned. However, once the object is recycled, it should not be accessed again, so that zombie object detection can be turned on, and the system will automatically check for zombie objects, but it will also reduce execution efficiency.memory leak: means that the object is not recycled at the time of the collection, but resides in memory until the end of the program is released Classification of memory management:->mrc:manual Reference Counting, both manual memory managementThe operation of reference counters is done by the programmer himself.->arc:automatic Reference Counting, both automatic memory management(garbage collection cannot be used on iOS systems and is not discussed at this point ) The system will automatically change the value of the reference counter according to the programmer's requirements (Xcode6 start, arc is used by default) Manual memory Management:Principles of Manual memory management:1) Once an object is created, the value of the reference counter for this object is 1, so you must match 1 release2) only retain when more than 1 people use this objectonly release when burning 1 people to use this object.3) The number of retain to match the number of release4) Never manually invoke the object's Dealloc method, but instead let the system automatically invokeseveral cases of memory leaks in manual memory management:1) Retain and release mismatch, retain excess release caused by memory leaks;
2) The object is not used in the process of being release, but is assigned to nil;3) Improper use of retain in the method;the key to manual memory management is to prevent memory leaks!!!!! To prevent memory leaks remember:1) who created "Alloc", "new", who "release";2) who "retain", who "release";Manual memory management for multiple objects:to prevent memory leaks when Class B is a property of Class A, the setter method for Class A should be:    The setter method implementation can also be written as:The above is the standard MRC Memory management code.cyclic retain problem:1) Problems encountered:when two objects are referenced by one another.
the property of the A object points to the B object, and the property of the B object points to the A object.at this time, if both sides use retain. There will be a memory leak. It can't be recycled.2) Solution:The 1 end uses the retain,1 end to use the assign. (see @property with parameter usage)It is important to note that in the 1 paragraphs of assign, the Dealloc is not required to release the object.Auto-Release pool (autorelease)1) Principle:An object stored in the auto-free pool, which automatically calls the release method of the object when the auto-free pool is destroyed, so the object is stored in the auto-free pool and no more write release is required2) Create a method:@autorelease{}//braces Indicate the extent of the auto-release pool3) To put the object into the method:The Autorelease method of the object is called in the scope of the auto-free pool. Note: The return value of Autorelease is the object itself, so we can create objects like this:@autorelease{Type * Object = [class name alloc] init] autorelease];}4) Use note:A. Only the Autorelease method of the object is called in the auto-release pool, and this object is stored in the auto-free pool.B. Object creation can be stored in this auto-free pool by invoking the Autorelease method of the object, outside of the auto-free pool, in the auto-free pool.c. When the auto-release pool ends. Simply send 1 release messages to objects stored in the auto-free pool instead of destroying objects.d. If the Autorelease method of the same 1 objects is called multiple times in the auto-free pool. The object is stored multiple times into the auto-free pool. At the end of the auto-release pool. Multiple release messages are sent for the object. Then there will be a zombie object error.E. The auto-free pool can be nested. The Autorelease method of the calling object, which tells the object to join the current auto-free pool, will only send the release message like an object when the current auto-free pool ends .5) Usage specification:we generally write 1 classes. We write 1 class methods of the same name for our class, which allows the outside world to invoke class methods to quickly get 1 objects. Specification: Objects created using the class method require that the object be autorelease in the method. As long as we call the class method to create the object in the auto-free pool, the created object is automatically added to the auto-release. Automatic memory management:Automatic Memory Management principle: The compiler will automatically insert retain, release, autorelase code in the appropriate place; The compiler automatically makes reference counts for objects. And as a developer, there's no need to worry that the compiler will do the wrong thing (unless the developer himself uses arc) .strong pointers and weak pointers:Strong pointers: By default, we declare pointers as strong pointers, or we can use __strong to display the declaration pointers as strong pointers.weak pointer: A pointer decorated with the __weak keyword, such as __weak person *p;Action and Difference: In arc mode, strong pointers and weak pointers are used as criteria for reclaiming objects, and when 1 objects are pointed with a weak pointer, but no strong pointers are pointed, they are immediately reclaimed, and the weak pointer is automatically set to nil.Circular references in->ARC mode:under the ARC mechanism, if a circular reference occurs, there are 1 properties in the A object that are B objects. There are 1 properties in the B object that are the A object. At this point, if both sides are strong. A memory leak occurs.Solution: 1 End Using strong 1 end using weak   conversion and compatibility of MRC and Arc:# ARC-compatible MRC classTarget-to- Build phaese---> Compiler Sources-Compiler Flagsmake the program compatible with ARC and non-arc parts.
change to non-Arc-fno-objc-arc
transformed into an arc,-f-objc-arc.# MRC turns arcxcode-> Refactor-to-Convert to objective-c ARC Select the target and calibration code to be swapped

OC Memory Management: MRC vs. 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.