About OC Memory Management-01

Source: Internet
Author: User

1. What is memory management?

We all know that the memory of the mobile phone is limited, the app's memory should also be limited, as the use of the app will lead to increased memory consumption, when the memory consumption reaches a level, the system will issue a memory warning, then we need to put some unused objects and variables occupied by the memory freed, That means we need to manage the memory manually. And we manage the scope: Any object that inherits the NSObject, is not valid for basic data types (such as float, int, char, struct, enum, and so on).


2. How to manage memory

1) Each OC object itself has a counter that consumes 4 bytes of memory, which stores an integer that represents "the number of times the current object is referenced" and defaults to 1 when the object is established (for example, Alloc, new, copy). The function of the counter is that when the object counter is 0 o'clock, the current object will be reclaimed by the system, if the counter is not 0, the whole process of execution of the program, the current object's memory is not back to be recycled





2) Three operations to reference counters

(1) Retain (Send message to object one retain message) counter +1, return value, return the object itself

(2) Release (Send Message to object one release message) counter-1, no return value.

(3) Retaincount(Send message to object a retaincount message) Gets the reference counter value of the current object

3. The system automatically calls the Dealloc method when the object is destroyed, and the Dealloc method is like a last word, so we generally rewrite the Dealloc method

And this method, must have [super Dealloc], and must be placed on the last side.

3. Suppose we have a person class

#import <foundation/foundation.h >

@interface Person:nsobject

{

{

int _age;

}

-(void) Setage: (int) age;

-(int) age;

}


#import "Person.h"

@implementation person

{

-(void) Setage: (int) age{


_age = age;

}



-(int) age{

return _age;

}

-(void) dealloc{

NSLog (@ "Person object is recycled");

[Super Dealloc];

}


(1) The first case

int main () {

When we call Alloc, the object counter defaults to 1. So when we have alloc, we need to add [object name release]

P-1 (current counter is 1)

Person *p =[[person alloc] init];

P-2 (current counter is 2)

[P retain];

Release indicates that the counter is minus 1, at which time p-1

[P release];

p-0, the system reclaims the object P and executes the Dealloc method of the object p.

[P release];

return 0;

}


(2) The second case


int main () {

When we call Alloc, the object counter defaults to 1. So when we have alloc, we need to add [object name release]

P-1 (current counter is 1)

Person *p =[[person alloc] init];

P-2 (current counter is 2)

[P retain];

Release indicates that the counter is minus 1, at which time p-1

[P release];

p-0, the system reclaims the object P and executes the Dealloc method of the object p.

[P release];


In particular, the system has recycled the object p, if we were to execute [P release] multiple times here.

Access to zombie objects (objects that have been reclaimed by the system, a piece of memory that is not available)

The p is then called the Wild Pointer (pointer to the zombie object), which causes bad access to the exc_bad_access

return 0;

}




(3) The third case


int main () {

When we call Alloc, the object counter defaults to 1. So when we have alloc, we need to add [object name release]

P-1 (current counter is 1)

Person *p =[[person alloc] init];

P-2 (current counter is 2)

[P retain];

Release indicates that the counter is minus 1, at which time p-1

[P release];

p-0, the system reclaims the object P and executes the Dealloc method of the object p.

If you do not open the Enable Zoombie Object, you will not get an error.

If you open it, you will get a prompt error

Message Send to deallocated instance

Send a message to an instance that has been recycled

P.age = 10;

return 0;

}

(4) The fourth case;

int main () {

When we call Alloc, the object counter defaults to 1. So when we have alloc, we need to add [object name release]

P-1 (current counter is 1)

Person *p =[[person alloc] init];

P-2 (current counter is 2)

[P retain];

Release indicates that the counter is minus 1, at which time p-1

[P release];

[P release];

Combined with the third case. That's what we'd think, when the counter was supposed to be 0, we could do retain, counter +1

We're not going to be able to execute p.age = 10; in fact, it is impossible to recover from the resurrection of the object.

The results of the execution are as follows.

Message Send to deallocated instance

Send a message to an instance that has been recycled

P.age = 10;

return 0;

}




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

About OC Memory Management-01

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.