Objective-c "Memory Management & Manual Memory Management Overview"

Source: Internet
Author: User

———————————————————————————————————————————
Memory management

(1) Memory management of OBJECTIVE-C



The stack area holds the local variable (because the basic data type occupies the storage space is fixed, by the system to allocate, we do not need to pipe, therefore the stack area holds the basic data type,)

The memory space allocated dynamically during the process of the heap storage program (the object type is dynamically allocated during the program's run, and their size is not fixed.) For example, we are the person new application, stored in the heap area, also we need to manage)

★ So the scope of memory management is to inherit all the objects of NSObject (objects in the heap area)
★ What is this for? Heap memory is not freed automatically (requires a memory management mechanism), and stack memory is automatically freed (auto-stack) after execution of the code

Reference counter (typically with%ld or%tu to see the value inside, unsigned)

Each object has its own reference counter, which is an integer representing the number of times the object has been referenced, that is, how many things are being used in this object, the value of the reference counter becomes 0 o'clock, and the object is destroyed

① Each object is just born, the reference counter is 1 (once the object is created, the default reference counter is 1)

② if the object's reference counter is not 0, the memory it consumes will not be recycled during the entire program's run (unless the entire program exits)

③ When you create an object using Alloc, new, or copy, the object's reference counter defaults to 1.


(2) OBJECTIVE-C Memory management classification

OBJECTIVE-C provides three ways to manage memory:

①mannul Reference Counting (MRC, manual management, we are responsible for using reference counting to manage memory, such as manual retain, release, Autorelease, and so on, when developing a version project prior to IOS 4.1. In the post-iOS version we can use ARC to let the system manage the memory itself)

②automatic Reference counting (ARC, auto reference count, IOS 4.1 launch)

③garbage Collection (garbage collection, iOS does not support garbage collection)

★ Apple official recommended that we use ARC technology to manage memory, MRC understanding is good


———————————————————————————————————————————
Manual memory Management (MRC)

(1) First of all we need to manually manage the memory (MRC) to turn off automatic management (arc off) first.

Ways to turn off arc:

Click items (Total, blue)-bulid setting--> basic level--> search objective-c Automatic Reference counting--> The four options appear, all set to No can

(2) We determine whether the object needs to be recycled, this is to see the value of the reference counter, you need to call
-(Nsinteger) Retaincount method (This is an object method with a return value, the return value is Nsinteger,nsinteger is a dynamic type, his type in different operating systems may be type int, or a long type, he is signed.) Nsuinerger is an unsigned type with no negative numbers)

Retaincount return value > 0 is not recycled
return value of Retaincount = 0 Reclaim memory space

★ Note: The return value of Retaincount here is%tu or%ld type ~

(3) When retrieving object memory space, a method is usually called automatically, Dealloc (releasing the object's properties)

We can rewrite Dealloc this method to see if the object space is not released (add a word output, if released will automatically call Dealloc, then this sentence will be output)

★ Note here that rewrite dealloc and rewrite init, first call the parent class's Dealloc method, that is [super dealloc]; , cause no more say

(4) There are two object methods that can increase the value of the reference counter of an object by 1 and decrease by 1

① use release to make reference count-1
② use retain to make reference count +1

(5) Memory management

We should have an idea in mind to keep the reference count of the object in a balanced state.

That is: retain + new = release (increase reference count = Reduce reference count)


Code:

#import <Foundation/Foundation.h>

@interface Person:nsobject
-(void) run;
@end

#import "Person.h"

@implementation person
-(void) dealloc
{
NSLog (@ "Object memory space released! ");
[Super dealloc];//This sentence must be placed in the rewrite dealloc the last sentence of the method, the meaning is: first release the memory space occupied by the subclass, and then release the memory space occupied by the parent class (and the overriding construction method is just the opposite, the initialization of the object is the first to execute the parent class init, Perform initialization of subclasses)
Also note, never call the Dealloc method directly, if the call should let Retaincount = 0, but if you write [P dealloc] also will not error, just not necessary! This is a sentence that does not need your own idle egg ache to write.
}
-(void) run
{
NSLog (@ "run!");
}
@end

int main (int argc, const char * argv[]) {
@autoreleasepool {
Person *p=[person new];

NSLog (@ "P->retaincount =%tu", [P retaincount]);
This place because P is created, so the number of references p is +1, so the output value is 1. But remember, the arc must be closed, if not closed, it will be an error.

[P run];
NSLog (@ "P->retaincount =%tu", [P retaincount]);
The output here is still 1, because [P run]; Just called the Run method, but the number of times P was quoted is 1.

[P release];
This will refer to the Count-1, so the reference count at this point is 0, that is, it will automatically call the Dealloc method, print our rewrite dealloc inside the "Object space release!" ”

[P run]; Object space is freed, but can still be called, what is this for? This question remains in the next section "Memory management of a single object (wild pointer) to explain"
}
return 0;
}


———————————————————————————————————————————

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Objective-c "Memory Management & Manual Memory Management Overview"

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.