Memory management of several ways (the collation of some of their own information, Daniel do not spray)

Source: Internet
Author: User
Tags garbage collection
OBJECTIVE-C provides three kinds of memory management methods:
Mrr:manual retain-release Manual Management
Arc:automatic Reference counting Automatic reference count
GC: (Mac OS only support) garbage collection garbage collection


MRR Manual management is based on reference counting, when we need to use a block of memory (object) to change the reference count of this memory, a reference count of memory if it is zero, then it has no user or caller, then it will be recycled;


MRR follows a few simple principles:
1. When you create objects (Alloc, new, copy ...). The initial reference count is 1;
2, if you need to refer to the object, you can send an retain message to the object, so that the object counter to add 1
3, when you do not need to refer to the object, you can send a release message to the object, so that the object counter is reduced by 1
4, when the counter is reduced to 0, automatically call the object's Dealloc function, the object will release memory
5, the counter is 0 of the object can no longer use release and other methods
6, do not release or overwrite the memory in use, will crash;
7, release the unused memory, to prevent memory leakage;


What Autoreleasepool is and how it works
When you send an autorelease message to an object, cocoa puts a reference to the object into the latest automatic release pool. It is still a legitimate object, so that other objects within the scope of the automatic release pool definition can send messages to it. When the program executes to the end of the scope, the automatic release pool is freed and all objects in the pool are freed.
1. Ojc-c is a "referring counting" (reference count) to manage memory, the object at the beginning of the allocation of memory (ALLOC) when the reference count is one, after each encounter with Copy,retain when the reference count will add one, The reference count is reduced whenever the release and Autorelease are encountered, and if the count of this object becomes 0, it is destroyed by the system.
2. NSAutoreleasePool is used to do reference counting of the management work, this thing is generally not in your control.
3. Autorelease and release no difference, just reference count minus one time is different, autorelease will be in the object's use of the real end of the reference count minus one.


What is the automatic release pool (autorelease) and how it works. (Yu Ying 5 times)
When you send an autorelease message to an object, cocoa puts a reference to the object into the latest automatic release pool. It is still a legitimate object, so that other objects within the scope of the automatic release pool definition can send messages to it. When the program executes to the end of the scope, the automatic release pool is freed and all objects in the pool are freed.
1. Ojc-c is a "referring counting" (reference count) to manage memory, the object at the beginning of the allocation of memory (ALLOC) when the reference count is one, after each encounter with Copy,retain when the reference count will add one, The reference count is reduced whenever the release and Autorelease are encountered, and if the count of this object becomes 0, it is destroyed by the system.
2. NSAutoreleasePool is used to do reference counting of the management work, this thing is generally not in your control.
3. Autorelease and release no difference, just reference count minus one time is different, autorelease will be in the object's use of the real end of the reference count minus one.


1. The automatic release pool is an automatic memory recovery mechanism of OC, which can be used to unify and recycle some variables or temporary variables by automatic releasing pool.
2. When the automatic release pool is destroyed, all the objects in the release pool will do the operation of releasing;
3, any OC object just call the Autorelease method, it will put the object to its closest automatic release pool (stack top of the release pool)

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.