OC Memory Management Summary (* * Output is the best learning * *)

Source: Internet
Author: User

    • Introduction

Memory Management: Clean up (recycle) unused memory so that memory can be reused.

This is a concern-how to determine that an object is no longer needed and that the memory it occupies can be recalled.

Prior to the release of Xcode4.2, OC Memory management was done by the programmer itself, which managed the memory count manually.

After the Xcode4.2 version, support for automatic reference counting (Automatic Reference Counting,arc), that is, to get rid of manual management, the programmer from the trouble of memory management problems completely freed, oyeah~~

However, to understand the legacy code of the past, it is necessary to understand the principle of manual memory management. And it can help coder to make more informed decisions about using memory.

Here, it is recommended to turn on the ARC function (in Build setting).

Two basic OC memory management modes supported by Apple:

    1. Manual memory Management: Manual reference counting and automatic pool release
    2. Auto Reference count Arc
    • Manual memory Management--manual reference counting

What is a reference count?

Each object has an integer associated with it, which is referred to as the object's reference count (4 bytes).

When there is code to access the object, the reference count is +1;

When no longer accessed, the reference count-1;

When an object is established, the reference count is initially = 1;

When the reference count = 0 o'clock, the object is over, is about to be destroyed, and its occupied memory is reclaimed by the system for reuse.

=================================================

Reference counting involves the method:

Create objects: When you create an object by using Alloc, new, or copy, Mutablecopy, the object's counter is set to 1.

At this point you can say, you, owning this object, you are responsible for the memory management (release) of the object.

Note that the method named according to the hump rule (lowercase first letter, the remainder of the first letter, such as Smallbig), prefixed with the above alloc, conforms to the rules for creating the object.

There are two ways of doing this:

1. Send the release message to the object.

2. Send an Autorelease message to the object to add it to the auto-release pool.

Object *obj1 = [[Object alloc] init]; // Statement [obj1 release];     *obj2 = [[[Object alloc] init] autorelease]; // Statement

Increase object access: to guarantee the existence of an object, whenever a reference to an object is created, the reference count is +1, at which point a retain message can be sent to the object.

Retain related methods:

-(ID// will object counter +1, return the object at the same time.)     -(Nsuinteger) Retaincount;  // returns the value of the current counter, with%lu in NSLog. 

Reduced object access: When an object is no longer accessed, a release message is sent to the object, counter-1.

[obj release];

Destroy object: When the object counter is 0, the system automatically calls Dealloc destroy the object, note that the system is automatic, do not manually call the Deallo method.

The Dealloc method cannot be called manually, but can be overridden.

For example, object A has object B, and in the Dealloc method of destroying object A, you need to add a statement that frees object A.

The next section of code is only for understanding dealloc rewrite meaning, if there are errors, please change, (*^__^*) ~ ~

@interface a:nsobject{   -(void) dealloc; @end @implementation a@sythensize B; -(void) dealloc{   [b dealloc];   [Super Dealloc]; // don't forget. }@end

PS: It is important to note that some of the methods in the foundation framework may increase the reference count of the object, such as Nsmutablearray's AddObject:, or UIView addsubview:. Some methods automatically reduce the reference count of objects, such as Removeobjectindex: and REMOVEFROMSUPERVIEWP:.

PS: If the object has been destroyed and the object reference is invalidated, the reference becomes a hanging pointer (dangling pointer), and if the release message is sent to the disposed object, the object is disposed excessively, causing the program to crash.

    • Manual memory management-automatic free pool

The autorelease pool can help track objects that need to be released for some time and are freed automatically. When you send an autorelease message to an object, you are actually adding the object to the auto-free pool. When the auto-free pool is destroyed, the release message is sent to all objects in the pool.

There are two ways to create an auto-free pool:

    1. by @autoreleasepool keyword
    2. by NSAutoreleasePool Object

@autoreleasepool {    //statement      *new]; // or pool = [[NSAutoreleasePool alloc] init]; // ... // [obj autorelease]; // ... [Pool release];

==========================================================

Cocoa Manual memory management rules *********

    • When you create an object using the new Alloc or Copy method, the object's retention counter is 1. When you no longer use the object, you should send a release or autorelease message to the object. In this way, the object will be destroyed at the end of its useful life.
    • When you get an object by other means, assuming that the object has a retention counter of 1 and that it has been set to be freed automatically, you do not need to do anything to ensure that the object is cleaned up. If you intend to have the object for a period of time, you need to keep it and make sure that it is released when the operation is complete.
    • If you keep an object, you need to eventually release or automatically release the object. The Retain method must be kept equal to the number of times the release method is used.

Detailed introduction See http://www.cnblogs.com/melody5417-bky/p/4314443.html

============================================================

OC Memory Management Summary (* * Output is the best learning * *)

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.