iOS Phase 20th day notes (memory management in OC)

Source: Internet
Author: User

iOS Learning (OC language) Knowledge Point finishing

First, the memory management in OC

1) Concept: Memory management object for all inherited NSObject objects, for basic data (such as: int, float, double ...) Invalid
The memory is managed by the reference counter in OC, which is an integer data representing the number of times the object is referenced, with each object allocating 4 bytes
Memory space to hold the reference counter. When an object has a reference counter of 0, it is automatically released, conversely, when using alloc,
The reference counter defaults to 1 when new and copy (Mutablecopy) is created

2) Golden rule when you use Alloc, new, copy (mutablecopy) to create an object to assign to a reference, when you no longer use this
When a reference is made, it is important to send a release (autorelease) message to release the memory occupied by the object.

3) The memory management described here is manual memory management, the new project is the system by default is automatic memory management, so you need to manually set, that is, in the project
Found in build Settings objective-c Automatic Reference counting entry set to No; You also need to add a pair of wild fingers
The control of the needle is found in the edit Scheme and the Enable Zoombie Objects item is checked.

3) Memory management operation Instance code:

1. Add a person class. h file without any action

2. In the. m file, the destructor destroys the memory execution method for example:

1 " Person.h " 2 @implementation Person 3 // destruction Memory execution method of destructor system 4 -(void) dealloc{5      NSLog (@ "persondealloc " ); 6 }7

3. Perform operations in the main file such as:

Person *p1=[[person Alloc]init];//Reference counter default +1[P1 retain];//Reference counter +1NSLog (@"retaincount:%ld", P1.retaincount);//Results: 2[P1 release];//Reference Counter-1//Retaincount Get the number of reference countersNSLog (@"retaincount:%ld", P1.retaincount);//Results: 1[P1 release]; NSLog (@"retaincount:%ld", P1.retaincount);//Results: 0


4) When using a combinatorial class, you need to destroy the combined object first, that is, destroy the combined object in the destructor method, for example:

1  // There is a book class in the person class that adds this method to the. m file of the person class 2    -(void) dealloc{3        [_book release]; 4        NSLog (@ "persondealloc"); 5     }


5) When combining class initialization, you need to add a set method to the parent class to increase the number of reference counters when assigning values to a composite class for example:

1     // the method of assigning the book class in the Person class 2      -(void) Setbook: (book*) book{3        book=[book retain]; 4      }56      //The value method of the book class in the Person class 7       -( book*) book{8           return  _book; 9      }


6) memory management in arrays: Destroying an Array object requires destroying the objects stored in the array first, for example:

1Nsmutablearray *array=[[Nsmutablearray alloc]init];2NSLog (@"%ld", array.retaincount);3  for(intI=0; i<5; i++) {4Book *b=[[book Alloc]init];5b.id=i+1;6 [Array addobject:b];7 }8NSLog (@"before array%ld", Array.retaincount);//Results: 19  for(intI=0; i<array.count; i++) {Ten [array[i] release]; One } A [Array release]; -NSLog (@"After array%ld", Array.retaincount);//Results: 0

7) Circular Reference memory management: For circular references We must change one of the object types from retain to assign type
Otherwise, the memory will not be completely freed.

8) Autorelease Auto-release pool: Puts the object into an auto-release pool, and when the auto-free pool is destroyed, it gives Chizi
All objects send the release message Autorelease method returns the object itself to the object after sending the autorelease message to the reference
The counter value does not change

iOS Phase 20th day notes (memory management in OC)

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.