Memory Management for iOS

Source: Internet
Author: User

The memory management partition of OBJECTIVE-C is that the base type is stored in the stack (the memory occupied by the stack is automatically freed by the system) and the object is stored in the heap. Because OBJC does not have a garbage collection mechanism (GC) that does not automatically free up memory in the heap, it requires the programmer to manually release it, or it can cause memory overflow. OBJC's memory management mechanism mainly includes: 1, reference counter 2, attribute parameter 3, automatic release pool.
1. Reference counter
xcode4.2 introduces an arc (Automatic Reference counting) automatic reference counting mechanism, which automatically adds memory release code to the code when the program compiles, so we can no longer add code that frees the memory. If you want to manage memory yourself, you need to turn off arc (Close arc in Xcode: Project Properties-build settings--Search "garbage" to find objective-c Automatic Reference Counting set to No).
OBJC does not have a garbage collection mechanism, so OBJC's memory management relies on object reference counters. Each object has a corresponding integer (Retaincount) after it is created, which is the reference counter. After the object is created, the reference counter is 1, and the reference counter of the object after the copy, retain, Alloc, and new of the call object is added 1 on the original basis, if the release of this object is called minus 1, if the reference counter equals 0, The system calls the object's Dealloc method to free memory and destroy the object. and uphold the "who created, who release" principle.
2, attribute parameters?
? such as: @property (Nonatomic,strong) Car *car Such properties, there is no manual getter and setter method will not cause a memory leak. Use the attribute @property to define an instance variable so that Xcode will automatically generate the setter and getter methods in the corresponding. m file. How does the attribute parameter manage memory?

Let's look at how the setter is implemented internally when the property is retain:

-(void) SetName: (nsstring) name{

if (_name!=name) {

[_name release];//first releases the original value

_name =[name retain];//New value is re-assigned in retain;

}

}


When the property is copy, how it is implemented inside the setter:

-(void) SetName: (nsstring) name{

if (_name!=name) {

[_name release];//first releases the original value

_name =[name Copy];

}

}
property is modified into copy setter method internal implementation

Getter methods can extend the life cycle of an object, and its internal code is as follows:

-(NSString *) name{

return [[_name retain] autorelease];

}

More secure notation for getter methods

These are the setter and getter methods generated in the system, which ensures that the memory is better and more secure to recycle
3. Automatic release of the pool?
In OBJC, there is also a mechanism for automatic memory release called "Auto-release Pool", which is a semi-automatic mechanism, some operations need to be set manually. Automatic memory release declares a block of code using the @autoreleasepool keyword, and if an object calls the Autorelase method at initialization time, when the code block executes, Objects that have called the Autorelease method in a block will automatically call the release method once. This has the effect of automatic release, while the object's destruction process is also delayed (Unified call release method).

Memory Management for iOS

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.