iOS memory management

Source: Internet
Author: User

memory management is divided into: MRC, ARC

MRC: Manually Manage reference counters ARC: Automatic management of reference counters The release principle of arc//is MRC, which will cause the counter to change. The object is disposed as long as no strong pointer is pointed to the object. +1:retain, alloc, copy -1:release (Immediate-1), Autorelease (encounter release pool)   Operation Reference Counter principle: There is alloc, retain, copy will have the corresponding release or autorelease, no words, no operation. operation of the Autoreleasepool role: An object that manages an automatically freed object (Autorelease). The system creates a default auto-free pool in the main thread of the application when the developer creates the project (the child threads are not created), and the release pool life cycle is the life cycle of the application. The developer adds an auto-free pool, using @autoreleasepool{}, with the life cycle inside the curly braces. If there are multiple auto-free pools, the auto-free pool is managed by the release pool closest to it. if the cycle and auto-release pooling operations are combined, the auto-free pool should be placed inside the loop, preventing the auto-generated temporary objects from being reclaimed in a timely manner.  When the user interacts with the application, the system automatically helps the user create an auto-release pool to manage the temporary objects that are generated during the interaction. As the interaction ends and the life cycle of the release pool ends, you should use the class method sparingly to create the object. Property Memory Management: Assign retain copy if the property declaration is using assign, memory management is not allowed. If you are using retain or copy, memory management is required, and you need to override the Dealloc method to send the release message to the instance variable that corresponds to the property in the Dealloc method. For example:@interface Person:nsobject@property (retain) nsstring *firstname;@property (retain) nsstring *lastname;@property (Assign, readonly) NSString *fullname;@end @implementation Person// ...-(void) Dealloc[_FirstName release];[_lastname release];[Super Dealloc];}@end Container Class (collection) memory management 1, the object is added to the container, the container object will be a reference count of the reservation, that is, reference count +1 (that is, retain) 2, remove the object from the container, the object reference count-1 (release) 3. When the container object is release, all object references in the container are counted-1 (release). Copy: to the System class (example: NSString ... The immutable object sends a copy message that references object +1 to the object, and the Retain function is the same. Sending copy to a mutable object of the system class creates an immutable copy, the original object reference count is unchanged, and the replica object reference count is +1. 3. Sending the mutablecopy message to the immutable object of the system class will get a mutable copy of the object, the original object reference count is unchanged, and the Copy object reference count is added one. 4. Sending a mutablecopy message to a mutable object of a system class gets a mutable copy of the object, the original object reference count is unchanged, and the Copy object reference count is added one. 5. The objects in the container are all pointer copies (shallow copies). 6. If the custom class is to implement the Copy method, the Nscopying protocol must be followed to implement the Copywithzone method.

iOS memory management

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.