IOS learning 20th-day notes (memory management in OC), iosoc

Source: Internet
Author: User

IOS learning 20th-day notes (memory management in OC), iosoc

Knowledge points of IOS Learning (oc language)

 

1. Memory Management in OC

1) concept: the memory management object is all objects that inherit NSObject and is invalid for basic data (such as int, float, double ...).
In OC, the reference counter is used to manage the memory. It is an integer that indicates the number of times the object is referenced. Each object is allocated 4 bytes.
Memory space to store reference counters. When the reference counter of an object is 0, it is automatically released,
When creating a new object, the reference counter is 1 by default.

2) When alloc, new, and copy (mutableCopy) are used to create an object and assign it to a reference
You must send the release (autoRelease) message to release the memory occupied by this object.

3) the memory management mentioned here is manual memory management. The system automatically manages the memory for new projects by default. Therefore, you need to set it manually.
In Build Settings, find the Objective-C Automatic Reference Counting item and set it to NO.
Click Edit Scheme to find and check Enable Zoombie Objects.

3) memory management operation instance code:

1. Add a Person class. h file without any operation

2. In the. m file, the destructor system destroys the memory. The execution method is as follows:

1 # import "Person. h "2 @ implementation Person3 // Method for destroying memory in the Destructor 4-(void) dealloc {5 NSLog (@" Person dealloc "); 6} 7 @ end

3. perform the following operations in the main file:

Person * p1 = [[Person alloc] init]; // reference counter + 1 [p1 retain] by default; // reference counter + 1 NSLog (@ "retainCount: % ld ", p1.retainCount); // result: 2 [p1 release]; // reference counter-1 // retainCount obtain the number of referenced counters NSLog (@ "retainCount: % ld", p1.retainCount ); // result: 1 [p1 release]; NSLog (@ "retainCount: % ld", p1.retainCount); // result: 0


4) when using a combination class to destroy its own objects, you must first destroy the combination objects, that is, destroy the combination objects in the destructor, for example:

1 // there is a Book class in the Person class. add this method 2-(void) dealloc {3 [_ book release]; 4 NSLog (@ "Person dealloc"); 5} to the m file}


5) When initializing a combination class, you must add the set Method to the parent class to add reference counters when assigning values to the combination class. For example:

1 // Method for assigning a value to the Book class in the Person class 2-(void) setBook :( Book *) book {3 book = [book retain]; 4} 5 6 // method for the Book class in the Person Class 7-(Book *) book {8 return _ book; 9}


6) memory management in the array: to destroy an array object, you must first destroy the objects stored in the array, for example:

1 NSMutableArray * array = [[NSMutableArray alloc] init]; 2 NSLog (@ "% ld", array. retainCount); 3 for (int I = 0; I <5; I ++) {4 Book * B = [[Book alloc] init]; 5 B. ID = I + 1; 6 [array addObject: B]; 7} 8 NSLog (@ "before array % ld", array. retainCount); // result: 1 9 for (int I = 0; I <array. count; I ++) {10 [array [I] release]; 11} 12 [array release]; 13 NSLog (@ "after array % ld", array. retainCount); // result: 0

 

7) memory management of cyclic references: for cyclic references, we must change one of the object types from retain to assign.
Otherwise, the memory cannot be completely released.

8) autorelease Auto Release pool: puts objects in an auto release pool. When the Auto Release pool is destroyed
All objects send the release message autorelease method. The returned object itself is referenced after the autorelease message is sent to the object.
Counter value unchanged

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.