------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------
Manual Memory Management
* Why learn memory management?
① because the memory of mobile devices is extremely limited, there is a limit to the amount of memory each app occupies.
② When we're on O BJECTIVE-C memory management mechanism elusive, programs that are written often leak or crash in memory.
③ when the app takes up a lot of memory, the system issues a memory warning, and you have to reclaim some memory space that you don't need to reuse.
*object-c Memory Management Scope
Manages only any objects that inherit nsobject, and does not manage other basic data types (int, char, float, double, struct, enum, and so on).
* Manual memory management solves two problems
① Avoid wild pointer operation
② Preventing memory leaks
1. Understanding Basic Memory Management Concepts
① Zombie Object: Objects occupied by memory have been reclaimed, zombie objects can no longer be used
② Wild pointer: pointer to zombie object (memory not available), send message to wild pointer error (exc_bad_access)
③ NULL pointer : There is no pointer to anything (the stored thing is nil,NULL,0 ), sending a message to a null pointer does not give an error
④ Reference counter: (int type, occupies 4 storage space). Each object has a reference counter, count=1 when the object is born, and when Count=0, the object is destroyed (recycled).
⑤ Retaincount: Returns the value of the object reference counter to see how many pointers point to the object.
⑥retain: The return value is the current object, Count = Reference counter + 1, and when a retain message is sent to the object, the reference counter does the plus 1 operation
⑦release: No return value, Count = Reference counter -1; When a release message is sent to the object, the object's reference counter does minus 1, and when the reference counter = 0, the system automatically reclaims the object.
⑧ when the reference counter = 0 O'Clock: The system automatically sends an Dealloc method to the object, notifies the object that it will be destroyed, its memory will be reclaimed, pointing to its pointer is not processed, the pointer's value is still present and becomes a wild pointer.
⑨alloc, new, copy to create the object, the value of the reference counter for the new object defaults to 1
2. Memory Management Principles
① in the object, whenever new, alloc, retain appear, you must have a release to pair
② who created the object, when it was no longer used, its release
③ who used the created object, its retain, and when the period is no longer used, its release
Example Demonstration:
<span style= "FONT-SIZE:12PX;" > #import <Foundation/Foundation.h> #import "ClassRoom.h" int main (int argc, const char * argv[]) { Classroom *student1 = [[Classroom alloc]init];//student1 used alloc, finally strdent1 call Release NSLog (@ "Student1 using Alloc, the reference counter is:% Zd ", [Student1 Retaincount]); Classroom *student2 = [Student1 Retain];//student2 used Retain,student2 no longer used, Student2 to release NSLog (@ " Student1 after using retain, the reference counter is:%zd ", [Student1 Retaincount]); [Student2 Release];//student2 sends the release message, and Student2 no longer uses the object [Student1 release];//student1 to send the release message, After that Student1 no longer uses the object return 0;} </span>
The classroom class overrides the Dealloc method as follows
-(void) dealloc{ NSLog (@ "object is released"); [Super Dealloc];}
result output:
2015-04-23 21:52:14.914 04-memory management [689:48749] student1 after using alloc, the reference counter is: 12015-04-23 21:52:14.915 04-memory management [689:48,749] Student1 after using retain, the reference counter is: 22015-04-23 21:52:14.916 04-memory management [689:48,749] object is disposed
3. Memory management of object combinations
Students, books are OC objects, when students have a book.
The key to the combination between students and books is the realization of students ' Setbook and Dealloc methods.
Set model
-(void) Setbook: (book *) book{ if (_book! = book) { [_book release]; _book = [book retain]; }}
Dealloc Model
-(void) dealloc{ [_book release]; [Super Dealloc];}
Example Demonstration:
MAIN.M file
#import <Foundation/Foundation.h> #import "Student.h" #import "Book.h" int main (int argc, const char * argv[]) {Stu Dent *student1 = [[Student alloc]init]; Student1.name = @ "Zhang San"; NSLog (@ "Student:%@ is created, its reference counter is:%zd", Student1.name,[student1 Retaincount]); Book *book =[[book Alloc]init]; Book.name = @ "Reader"; NSLog (@ "%@" was created, its reference counter is:%zd ", Book.name,[book Retaincount]); Student1.book = Book; NSLog (@ "%@" by students:%@ use, its reference counter is:%zd ", Book.name,student1.name,[book Retaincount]); Book *book1 = [[book Alloc]init]; Book1.name = @ "Liang Jian"; NSLog (@ "%@" was created, its reference counter is:%zd ", Book1.name,[book1 Retaincount]); Student1.book = Book1; NSLog (@ "%@" by students:%@ use, its reference counter is:%zd ", Book1.name,student1.name,[book1 Retaincount]); NSLog (@ "%@ use"%@ ", Discard"%@ ","%@ "Reference counter is:%zd", Student1.name,book1.name,book.name,book.name,[book Retaincount]); [Book1 Release];//book1 uses alloc once, so release once [book Release];//book use Alloc once, so release once [Student1 Release];//stud Ent1 Use alloc once, so to release aSecond return 0;}
Book.h File
#import <Foundation/Foundation.h> @interface book:nsobject{ @protected nsstring *_name;} -(void) SetName: (NSString *) name;-(NSString *) name; @end
BOOK.M file
#import "Book.h" @implementation book-(void) SetName: (NSString *) name{ if (_name!=name) { [_name release]; _name = [name retain]; }} -(NSString *) name{ return _name;} -(void) dealloc{ [_name release]; [Super Dealloc]; NSLog (@ "%@" destroyed, free memory ", _name);} @end
Student.h file
#import <Foundation/Foundation.h> @class Book; @interface student:nsobject{ nsstring *_name; Book *_book;} -(void) SetName: (NSString *) name;-(void) Setbook: (Book *) book;-(NSString *) name; @end
STUDENT.M file
#import "Student.h" #import "Book.h" @implementation student-(void) SetName: (NSString *) name{ if (_name! = name) {
[_name release]; _name = [name retain]; }} -(void) Setbook: (book *) book{ if (_book! = book) { [_book release]; _book = [book retain]; }} -(NSString *) name{ return _name;} -(void) dealloc{ [_name retain]; [_book release]; NSLog (@ "%@ is destroyed, free memory", _name); [Super Dealloc];} @end
result output:
2015-04-23 23:13:03.247 04-memory management [1278:74,338] student: Zhang San was created with a reference counter of: 12015-04-23 23:13:03.248 04-memory management [1278:74,338] The reader was created, Its reference counter is: 12015-04-23 23:13:03.248 04-memory management [1278:74,338] The reader is used by the student: Zhang San, whose reference counter is: 22015-04-23 23:13:03.248 04-Memory management [ 1278:74,338] "Liang Jian" was created, its reference counter is: 12015-04-23 23:13:03.248 04-memory management [1278:74,338] "Liang Jian" by the students: Zhang San use, its reference counter is: 22015-04-23 23:13:03.249 04-memory management [1278:74,338] Zhang three uses "Liang Jian", discarded "reader", "reader" reference counter is: 12015-04-23 23:13:03.249 04-memory management [1278:74,338] "reader" was destroyed, Free memory 2015-04-23 23:13:03.249 04-memory management [1278:74,338] "Liang Jian" was destroyed, free memory 2015-04-23 23:13:03.249 04-memory management [1278:74,338] Zhang San destroyed, free memory
basic usage of 4.autorelease
①autorelease is a semi-automatic reclamation of the object method that frees memory, is to put objects in the automatic release pool, when the automatic free pool is destroyed, all OC objects using autorelease do a release operation.
②autorelease is the object method, the return value is the OC object, and the object reference counter does not change after the autorelease is called.
Advantages of 5.autorelease
① do not focus on when the object is
② don't have to think about when to write release
6.autorelease use case
Memory-intensive objects, not free to use autorelease
7.autorelease Common error notation
After the ① object calls Autorelease, it also calls release
② calls Autorelease multiple times in a row
Common Error 1 @autoreleasepool { Student *stu = [[[Student alloc]init]autorelease]; [Stu release]; } Common Error 2 @autoreleasepool { Student *stu = [[[Student alloc]init]autorelease]; Student *stu1 = [Stu autorelease]; }
1.set method Memory management-related parameters
* Retain:release old value, retain new value (for OC object type)
* Assign: Direct assignment (default, for non-OC object types)
* Copy:release old value, copy new value
2. Whether to generate a set method
* ReadWrite: Generate both setter and Getter Declaration, implementation (default)
* ReadOnly: Only generate getter declarations, implementations
3. Multithreading Management
* Nonatomic: High performance (usually with this)
* Atomic: Low performance (default)
The name of the 4.setter and Getter methods
* Setter: Determines the name of the set method and must have a colon:
* Getter: Determines the name of the Get method (typically used in bool type)
The above example code modifies the following example:
MAIN.M file
#import <Foundation/Foundation.h> #import "Student.h" #import "Book.h" int main (int argc, const char * argv[]) {@au Toreleasepool {Student *student1 = [[[Student alloc]init]autorelease]; Student1.name = @ "Zhang San"; NSLog (@ "Student:%@ is created, its reference counter is:%zd", Student1.name,[student1 Retaincount]); Book *book =[[[book alloc]init]autorelease]; Book.name = @ "Reader"; NSLog (@ "%@" was created, its reference counter is:%zd ", Book.name,[book Retaincount]); Student1.book = Book; NSLog (@ "%@" by students:%@ use, its reference counter is:%zd ", Book.name,student1.name,[book Retaincount]); Book *book1 = [[[Book Alloc]init]autorelease]; Book1.name = @ "Liang Jian"; NSLog (@ "%@" was created, its reference counter is:%zd ", Book1.name,[book1 Retaincount]); Student1.book = Book1; NSLog (@ "%@" by students:%@ use, its reference counter is:%zd ", Book1.name,student1.name,[book1 Retaincount]); NSLog (@ "%@ use"%@ ", Discard"%@ ","%@ "Reference counter is:%zd", Student1.name,book1.name,book.name,book.name,[book Retaincount]); } return 0;}
Book.h file
#import <Foundation/Foundation.h> @interface book:nsobject@property (retain,readwrite,nonatomic) NSString * Name: @end
book.m File
#import "Book.h" @implementation book-(void) dealloc{ NSLog (@ "%@" is released, Memory Recycled ", _name); [Super Dealloc];} @end
Student.h file
#import <Foundation/Foundation.h> @class Book; @interface student:nsobject@property (Retain,readwrite, nonatomic) NSString *name; @property (retain,readwrite,nonatomic) book *book; @end
STUDENT.M file
#import "Student.h" #import "Book.h" @implementation student-(void) dealloc{ [_book release]; NSLog (@ "Student:%@ released, Memory Recovery", _name); [Super Dealloc];} @end
result output:
2015-04-23 23:54:53.554 04-memory management [1606:87,943] student: Zhang San was created with a reference counter of: 12015-04-23 23:54:53.556 04-memory management [1606:87,943] The reader was created, Its reference counter is: 12015-04-23 23:54:53.556 04-memory management [1606:87,943] The reader is used by the student: Zhang San, whose reference counter is: 22015-04-23 23:54:53.556 04-Memory management [ 1606:87,943] "Liang Jian" was created, its reference counter is: 12015-04-23 23:54:53.556 04-memory management [1606:87,943] "Liang Jian" by the students: Zhang San use, its reference counter is: 22015-04-23 23:54:53.556 04-memory management [1606:87,943] Zhang three uses "Liang Jian", discarded "reader", "reader" reference counter is: 12015-04-23 23:54:53.556 04-memory management [1606:87,943] The reader was released, Memory Recycling 2015-04-23 23:54:53.556 04-memory management [1606:87,943] "Liang Jian" was released, memory recovered 2015-04-23 23:54:53.557 04-memory management [1606:87,943] Students: Zhang San was released, Memory Recycling
Dark Horse programmer--OC Memory Management