IOS classic interview Summary-memory management, ios questions-memory management

Source: Internet
Author: User

IOS classic interview Summary-memory management, ios questions-memory management
I made a summary based on my own situation. The answer is my summary. If you have any bad answers, I hope to criticize, correct, and communicate. Thank you!Memory Management1. What is ARC?ARC is the automatic reference count of automatic reference counting, which is automatically added to retain/release during program compilation. When the object is created, retain count + 1, count-1 when the object is release, and destroy the object when count = 0. The autoreleasepool object added to the program will be automatically added by the system with the autorelease method. If the reference count of this object is 0, the object will be destroyed. Therefore, ARC was born to solve the problems of MRC manual memory management. Disadvantages of Memory Management in MRC: When releasing a heap memory, you must first determine that all pointers to the heap space are release. (Avoid releasing in advance) to release the heap space pointed to by the pointer, you must first determine which points to the same heap. These pointers can only be released once. (Avoid multiple releases, causing memory leakage) during modular operations, the object may be created and used by multiple modules, and it cannot be determined who finally releases the multi-thread operation, you are not sure which thread is used up. Although ARC brings a lot of programming results, it may also cause memory leakage. For example, in the following two cases: circular reference: A has an attribute that references B and B has an attribute that references A. If both are strong references, neither of the two objects can be released. Endless loop: If a ViewController has an infinite loop, the ViewController cannot be released even if the view corresponding to the ViewController disappears.2. The block is usually modified with that keyword. Why?Block uses the copy key for modification. block uses copy as a "traditional" legacy from MRC. In MRC, the block of method content is in the stack area, copy can be used to place it in the heap area. But write or not write in ARC: the compiler automatically performs the copy operation on the block.3. NSString (or NSArray, NSDictionary) declared with @ property often uses the copy keyword. Why? If you use the strong keyword, what problems may occur?A: @ property is used to declare NSString, NSArray, and NSDictionary. copy keywords are often used because they have the corresponding variable types: NSMutableString, NSMutableArray, and NSMutableDictionary. values may be assigned between them, to ensure that the string values in the object do not change unintentionally, copy a copy when setting the new attribute value. If we use strong, this attribute may point to a mutable object. If this variable object is modified externally, this attribute will be affected. Copy is similar to strong. However, the setting method does not keep new values, but copies them ). When the attribute type is NSString, this feature is often used to protect its encapsulation, because the new value passed to the setting method may point to an instance of the NSMutableString class. This class is a subclass of NSString, indicating a string that can modify its value. If the string is not copied, after setting the attribute, the string value may be changed without the knowledge of the object. Therefore, we need to copy an immutable string to ensure that the string value in the object will not be inadvertently changed. As long as the object used to implement the property is mutable, you should copy it when setting the new property value.4. runloop, autorelease pool, and the relationship between threads.Each thread (including the main thread) has a Runloop. For each Runloop, the system will implicitly create an Autorelease pool, so that all the release pools will constitute a stack structure like callstack. At the end of each Runloop, the Autorelease pool at the top of the current stack will be destroyed, so that each Object in the pool will be release.5. What is the essence of @ property? How ivar, getter, and setter are generated and added to this class."Property" has two major concepts: ivar (instance variable) and access method (access method = getter), I .e. @ property = ivar + getter + setter. For example, the following class: @ interface WBTextView: UITextView @ property (nonatomic, copy) NSString * placehold; @ property (nonatomic, copy) UIColor * placeholdColor; after the @ end class completes the attribute setting, the compiler will automatically write a method to access these attributes (automatically synthesize autosynthesis). The class written in the above Code is equivalent to the following code: @ interface WBTextView: UITextView-(NSString *) placehold;-(void) setPlacehold :( NSString *) placehold;-(UIColor *) placeholdColor;-(void) setPlaceholdColor :( UIColor *) placeholdColor; @ end For details, see: http://blog.csdn.net/jasonjwl/article/details/494273776. Write a setter method to complete @ property (nonatomic, retain) NSString * name and @ property (nonatomic, copy) NSString * nameThe setter method of the retain attribute is to retain the new value and release the old value, and then update the instance variable so that it points to the new value. Order is important. If the new value is not retained, the old value is released first, and the two values point to the same object, the release operation executed first may cause the system to permanently recycle the object. -(Void) setName :( NSString *) name {[name retain]; [_ name release]; _ name = name ;}- (void) setName :( NSString *) name {[_ name release]; _ name = [name copy];}7. Differences between assign vs weak and _ block vs _ weakAssign applies to basic data types. weak is applicable to NSObject objects and is a weak reference. The assign page can be used to modify objects. Why not use it? Because after the objects modified by assign are released, the pointer address still exists, that is, the pointer is not set to nil. If the address is allocated in the subsequent memory allocation, the program will crash. When the weak-modified object is released, the pointer address is set to nil. _ Block is used to modify a variable. This variable can be modified in the block. _ Block: variables modified with _ block will be retain in the block code block (under ARC, MRC won't retain) _ weak: variables modified with _ weak are not retain in the block code block.8. Check whether the following code is correct. If any, modify the code?@ Autoreleasepool {for (int I = 0; I [largeNumber; I ++) person * per = [[Person alloc] init]; [per autorelease] ;}} principle of Memory Management: If you use alloc, copy, and retain for an object, you must use the corresponding release or autorelease. At first glance, there should be alloc and autorelease in this question. The two should be correct. However, although autorelease reduces the reference count by one, it does not immediately subtract one. Its essential function is to put the object in the automatically released pool closest to it. When the automatic release pool is destroyed, the release message is sent to every object in the automatic release pool. The question is autorelease. Because largeNumber is a large number and autorelease cannot reduce the reference count by one immediately, memory overflow may occur before the loop ends. Solution: @ autoreleasepool {for (int I = 0; I [100000; I ++) @ autoreleasepool {Person * per = [[Person alloc] init]; [per autorelease] ;}} add an automatic release pool in the loop, in this way, each created object can be released in time.9. Is there any problem with the following code? If there is any problem, modify it?@ Autoreleasepool {NSString * str = [[NSString alloc] init]; [str retain]; [str retain]; str = @ "jxl"; [str release]; [str release]; [str release];} This question has the same memory leakage problem as the 8th question. 1. memory leakage 2. objects that point to the constant area cannot be release. The pointer variable str originally points to a new heap zone space, but after a new value is assigned to str, the direction of str changes from the original point to the heap zone space to the constant zone. Variables in the constant zone do not need to be released at all, which leads to the unreleased heap zone space and Memory leakage.10. Under what circumstances does the weak keyword differ from assign? When is the weak keyword used?In ARC, when circular references may occur, it is often necessary to allow one end to use weak. For example, if the delegate proxy itself has made a strong reference to it, there is no need to make a strong reference again. In this case, weak is also used, and weak is also used for custom control attributes. Difference: weak indicates that this attribute defines a non-having relationship ". When a new value is set for this attribute, the setting method neither retains the new value nor releases the old value. This feature is the same as assign. However, when the object referred to by the attribute is destroyed, the attribute value is also cleared. Assign's "setting method" only performs a simple value assignment for the "pure type" (scalar type, such as CGFloat or NSlnteger. Assign can use non-OC objects, while weak must be used for OC objects.11. Memory Management semantics (differences between assign, strong, and weak)Assign "Setting Method" only performs simple value assignment for "pure volume. Strong indicates that this property defines a "ownership relationship ". When a new value is set for this attribute, the setting method first retains the new value, releases the old value, and then sets the new value. Weak indicates that this attribute defines a "non-ownership relationship ". When a new value is set for this attribute, the setting method neither retains the new value nor releases the old value. This feature is similar to assign. However, when an object referred to by an attribute is destroyed, the attribute value is also cleared. Unsafe_unretained: the semantics of this trait is the same as assign, but it applies to "Object Type", which expresses a "non-having relationship". When the target object is destroyed, attribute values are not automatically cleared, which is different from weak. Copy is similar to strong. However, the setting method does not retain the new value, but instead copies the new value ". When the attribute type is NSString *, this feature is often used to protect its encapsulation, because the new value passed to the setting method may point to an instance of the NSMutableString class. This class is a subclass of NSString, indicating a string that can modify its value. If the string is not copied, after setting the attribute, the string value may be changed without the knowledge of the object. Therefore, we need to copy an "immutable" string to ensure that the string value in the object will not be inadvertently changed. As long as the object used to implement the attribute is "mutable", you should copy it when setting the new attribute value.

Q: customized IT education platform, one-to-one service, Q & A. Official Website for developing programming social headlines: www.wenaaa.com

QQ Group 290551701 has gathered many Internet elites, Technical Directors, architects, and project managers! Open-source technology research, welcome to the industry, Daniel and beginners interested in IT industry personnel!

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.