ARC and MRC in the iOS-ten "faces" ambush-interview magic books
I have been in touch with iOS for some time. From the first time I was a newbie who could only write NSLog, to a simple page layout that could use the basic control layout, I learned how to write core animations, every step is very pleased. I encountered various problems and various inexplicable "crashes" on the way. Fortunately, I was never frustrated. On the contrary, I was very active in the face of bugs and crashes, the reason is that I am very focused when looking for bugs and problems. I seem to be able to feel the brain current flow in my brain, although sometimes there will inevitably be awkwardness and despair at the end of the road, every time there is a solution in the village, it will feel that everything is worth it, full of smiles. I recently reviewed a lot of questions related to the interview. On the one hand, I checked what I learned during this period, and on the other hand I reviewed the knowledge network to check for missing information. In the eyes of one thousand people, there are one thousand hamplets. Apart from the official documents of Apple as a template, we all respect each developer. As more developers, we all have our own views, or mature or naive, can always be good. The blog content may be relevant or similar in the blog posts of other bloggers, but I think a good iOS developer should have his own opinions and write a blog as a record and accumulation of his long learning history. Until now, the main question is: Memory Management Mechanism (ARC) and MRC. -= -- =-= -= -=-iAronTalk Blog opens. even if the present, the match does not stop changes the page. -= -- =-= -= -=-Part 1: interview question set (accumulation-ing) =-01. what are the default keywords when no attribute keyword is explicitly specified in ARC? Answer: atomic, readwrite, strong (object), assgin (basic data type ). =-02. In objc, how does ARC/MRC perform memory management? Answer: See the following. Part 2: The system creates a large number of objects when the program runs. Like most advanced languages, objects in ObjC are stored in the heap, and the system does not automatically release the heap memory, note that the basic types are managed by the system and placed on the stack. If an object is not promptly released after it is created and used, it will occupy a large amount of memory. The accumulation of memory slows down the system and serious programs will crash. Other advanced languages such as C # and Java solve this problem through garbage collection (GC), but there is no similar garbage collection mechanism in OjbC, therefore, Apple came up with the reference count concept to manage objects in the memory. Before SDK4.0, MRC was used, that is, manual memory management, after 4.0, Apple introduced ARC (Automatic Memory Management), which greatly reduced developers' time. In the past, the labor-and-effort manual memory management era gradually moved away, however, when encountering the underlying Core Foundation object, you need to manually manage the reference count. About memory management: "memory space opened up when the program is running. Use it to release it. Write a program to use as little memory as possible. In Objective-C, memory management is seen as: "How to allocate ownership of restricted memory resources under a lot of data and code ". When you complete your program based on this guide, you will get the knowledge of "managing the life cycle of objects explicitly, releasing them when they are not used, and managing program memory. Looking at the memory management throughout iOS development, the deep layer is the rational retention and release of objects in the memory, but the superficial level is undoubtedly the reference counting world. We have never directly performed any operations on the memory, and all operations start with reference counting, and end with reference counting. Both ARC and MRC perform different operations on the reference count. =-Section A: MRC (MannulReference Counting) in Objective-C language ** 01. in MRC memory management mode, the methods related to variable management include retain, release, and autorelease. The retain and release methods operate on the reference count. When the reference count is zero, the memory is automatically released. Additionally, you can use the NSAID utoreleasepool object to manage the variables that are added to the autorelease pool, and reclaim the memory when drain is enabled. (1) retain. The function of this method is to attach the ownership of the memory data to another pointer variable, and Add 1 to the reference number, that is, retainCount + = 1; (2) release, this method releases the pointer variable's ownership of the memory data. The number of references minus 1, that is, retainCount-= 1; (3) autorelease, this method is to put the management of the object memory into the autoreleasepool. Sample Code: // assume that Person is a pre-defined class Person * person = [[Person alloc] init]; [person retain]; // reference count + 1 at this time, now 2 [person release]; // The reference count of the ownership of the memory data released by person-1, now 0; [person run]; // The memory is released when a wild pointer is displayed. ** 02. // use autoreleasepool in MRC management mode. The latest method is @ autoreleasepool {Person * person = [[Person alloc] init]; [person autorelease]; // autoreleasepool is used to manage the memory release} ** 03. identifier @ property (nonatomic/atomic, retain/assign/copy, readonly/readwrite) Number * num; 1) nonatomic/atomic, indicates whether this attribute is secure for multiple threads, whether to use the thread lock. the default value is atomic. 2) retain/assign/copy. It is about memory management for this attribute. l assign "is the default. in the setter that is crea Ted by @ synthesize, the value willsimply be assigned to the attribute, don't operate the retain count. myunderstanding is that "assign" shocould be used for non-pointer attributes. l "retain" is needed when the attribute is a pointer to an object. the setter generated by @ synthesize will retain (aka add a retain count) the object. you will need torelease the object when you are finished with it. l "co Py "is needed when the object is mutable. use this if you need the value of theobject as it is at this moment, and you don't want that value to reflect anychanges made by other owners of the object. you will need to release the objectwining you are finished with it because you are retaining the copy. (3) readwrite/readonly-"readwrite" is the default. when you @ synthesize, both a getter and asetter w Ill be created for you. if you use "readonly", no setter willbe created. use it for a value you don't want to ever change after the instantiationof the object. =-Section B: ARC (AutomaticReference Counting) in Objective-C language ** 01. in ARC, memory management-related identifiers can be divided into variable identifiers and attribute identifiers. For variables, the default value is _ strong, and for properties, the default value is unsafe_unretained. Autoreleasepool also exists. The variable identifier is: 1) _ strong, is the default. an object remains "alive" as long as there is a strong pointerto it. 2) _ weak, specifies a reference that does not keep the referenced object alive. A weakreference is set to nil when there are no strong references to the object. 3) _ unsafe_unretained, specifies a reference that does not keep the referenced object alive and is notset to nil when there are no s Trong references to the object. if the object itreferences is deallocated, the pointer is left dangling. 4) _ autoreleasing, is used to denote arguments that are passed by reference (id *) and areautoreleased on return, managedby Autoreleasepool. ** 02. usage of variable identifiers: _ strong Person * person = [[Person alloc] init]; In the ARC memory management mode, its attribute identifiers include the following: ** 03. @ property (nonatomic/atomic, assign/retain/strong/weak/unsa Fe_unretained/copy, readonly/readwrite) Number * num; // The default value is unsafe_unretained. Among them, assign/retain/copy has the same meaning as the property identifier in MRC, and strong is similar to retain, assign is similar to unsafe_unretained. strong/weak/unsafe_unretained has the same meaning as the variable identifier under ARC. It is just an identifier used for attributes and a variable identifier (with two dashes __). The other identifiers listed are of the same meaning as those listed in MRC. (1) For assign, you can use this attribute for scalar type (such as int. You can imagine a float. It is not an object, so it cannot be retain or copy. (2) For copy, specify that the object's copy (deep copy) should be used, and the previous value will send a release message. Basically like retain, but does not increase the reference count, it is to allocate a new memory to place it. This is especially applicable to NSString. If you do not want to change the existing one, use this because NSMutableString is also an NSString. ** 04. for Core Foundation and objective-cObject, the ARC management mechanisms required are: 1) (_ bridge_transfer <NSType>) op oralternatively CFBridgingRelease (op) isused to consume a retain-count of a CFTypeRef whiletransferring it over to ARC. this cocould also be represented by id someObj = (_ bridge <NSType>) op; CFRelease (op); 2) (_ bridge_retained <CFType>) op oralternatively CFBridgingRetain (op) isused to hand an NSObject ov Erto CF-land while giving it a + 1 retain count. you shoshould handle a CFTypeRefyoucreate this way the same as you wowould handle a result of CFStringCreateCopy (). this coshould also be represented by CFRetain (_ bridge CFType) op); CFTypeRef someTypeRef = (_ bridge CFType) op; 3) _ bridge justcasts between pointer-land and Objective-C object-land. if you have noinclination to use the conversions above, use t His one. =-Section C: Objectiv uses analysis tools to debug memory problems and locate code problems during compilation. Use the Clang Static Analyzer embedded in Xcode. If memory management problems still occur, you can use other tools and technologies to identify and diagnose problems. 1. Most tools and technologies are described in TN2239. iOS Debugging Magic, especially NSZombie, helps find over-released objects. 2. Use Instruments to track reference counting events and find memory leaks. (Refer to Collecting Data on Your App)