OC memory management-01, OC memory management-01

Source: Internet
Author: User

OC memory management-01, OC memory management-01

1. What is memory management?

We all know that the memory of mobile phones is limited, and the memory of app applications should also be limited. With the use of app applications, the memory usage will increase, when the memory usage reaches a certain level, the system will issue a memory warning. In this case, we need to release the memory occupied by some unused objects and variables, that is to say, we need to manually manage the memory. The scope of our management: any object that inherits NSObject is invalid for basic data types (such as float, int, char, struct, and enum.


2. How to manage memory

1) Each OC object itself has a counter that occupies 4 bytes of memory. It stores an integer that indicates the number of times the current object is referenced ", when an object is created (such as alloc, new, and copy), the default value is 1. When the counter of an object is 0, the current object is recycled by the system. If the counter is not 0, the memory of the current object will not be recycled.





2) Three operations for referencing a counter

(1) retain (send a retain message to an object) Counter + 1, with a return value, the returned object itself

(2) release (send a message to an object) Counter-1, no return value.

(3) retainCount (send a retainCount message to the object) to obtain the reference counter value of the current object

3. When the object is destroyed, the system will automatically call the dealloc method. The dealloc method is like the last words, so we generally rewrite the dealloc method.

In this method, [super dealloc] must be available and placed at the end.

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 recycled ");

[Super dealloc];

}


(1) first case

Int main (){

// When we call alloc, the default object counter is 1. Therefore, when alloc is available, we must add the [Object Name release]

// P-1 (the current counter is 1)

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

// P-2 (current counter is 2)

[P retain];

// Release indicates that the counter is reduced by 1. P-1

[P release];

// P-0, then the system will recycle object p, execute the dealloc method of object p

[P release];

Return 0;

}


(2) Case 2


Int main (){

// When we call alloc, the default object counter is 1. Therefore, when alloc is available, we must add the [Object Name release]

// P-1 (the current counter is 1)

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

// P-2 (current counter is 2)

[P retain];

// Release indicates that the counter is reduced by 1. P-1

[P release];

// P-0, then the system will recycle object p, execute the dealloc method of object p

[P release];


// In particular, object p has been recycled by the system. If we execute [p release] multiple times here

// Access the zombie object (the object that has been recycled by the system, an unavailable memory)

// P is called a wild pointer (pointer to a zombie object), which causes bad access, namely, EXC_BAD_ACCESS.

Return 0;

}




(3) Case 3


Int main (){

// When we call alloc, the default object counter is 1. Therefore, when alloc is available, we must add the [Object Name release]

// P-1 (the current counter is 1)

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

// P-2 (current counter is 2)

[P retain];

// Release indicates that the counter is reduced by 1. P-1

[P release];

// P-0, then the system will recycle object p, execute the dealloc method of object p

// If you do not open Enable Zoombie Object, no error is returned.

// If it is enabled, the following error occurs:

// Message send to deallocated instance

// Send messages to recycled instances

P. age = 10;

Return 0;

}

(4) fourth case;

Int main (){

// When we call alloc, the default object counter is 1. Therefore, when alloc is available, we must add the [Object Name release]

// P-1 (the current counter is 1)

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

// P-2 (current counter is 2)

[P retain];

// Release indicates that the counter is reduced by 1. P-1

[P release];

[P release];

// Combined with the third case. We will think like this, then the counter is originally 0, we can execute retain, counter + 1

// Can we execute p. age = 10 successfully? In fact, it is impossible to recycle objects.

// The execution result is as follows.

// Message send to deallocated instance

// Send messages to recycled instances

P. age = 10;

Return 0;

}




Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.