OC Base Memory Management
We understand the C language memory management, as follows:
(1) C Language memory allocation: char *p = (char*) malloc (100*sizeof (char));
(2) C-language memory release: Free (p);
Defects in C-language memory management:
(1) No release will cause a memory leak.
(2) Multiple releases can cause crashes.
(3) When many people in a large project work on a piece of memory, they cannot free up memory because they do not know if others are still using it.
Memory management in OC Language 1. Memory management
(1) Reference count.
(2) The Golden Rule of memory management: who creates who to release.
Let's take a look at the test example:
#import<Foundation/Foundation.h>#import "Student.h"intMainintargcConst Char*argv[]) {@autoreleasepool {//Memory Management//use reference counting to manage://alloc/retain/copy reference count plus 1//Release reference count minus 1//You can call release only if you have used Alloc/retain/copy .//The golden rule of memory management-who creates who releasesStudent*stu =[[Student alloc] init]; [Stu retain]; [Stu retain]; NSLog (@"%ld", [Stu Retaincount]); Student*stu4 =[Stu Copy]; NSLog (@"%ld", [Stu Retaincount]); NSLog (@"%ld", [Stu4 Retaincount]); NSLog (@"%p", Stu); NSLog (@"%p", STU4); } return 0;}
Test results:
2. Use setter in compound of class
Student class, Student.h file:
#import <Foundation/Foundation.h>@interface student:nsobject@end
STUDENT.M file:
#import " Student.h " @implementation Student-(NSString *) description{ return@ " I am a student " ;} @end
Teacher class, Teacher.h file:
#import <Foundation/Foundation.h>#import"Student.h"@ Interface teacher:nsobject{ *_stu;} -(void) Setstu: (Student *) stu; -(Student *) Getstu; -(void) printf; @end
TEACHER.M file:
#import "Teacher.h"@implementationTeacher//Error Method 1://because the Stu object is released, the _stu in teacher points to an unknown address, no longer points to the original Stu, so Stu needs retain, get Error Method 2//-(void) Setstu: (Student *) Stu//{//_stu = stu;//}//Error Method 2://because the STU1 memory address that the original _stu points to is not released when the value is assigned to another STU2 object, a memory leak is caused. So before retain need to release the old address space, get the error method 3//-(void) Setstu: (Student *) Stu//{//_stu = [Stu retain];//}//Error Method 3://if the calling Setstu incoming object pointer is the same object, then [_stu release], Stu point to the memory address of the reference count has become 0, the memory is freed, can no longer retain, so get the correct method 4//-(void) Setstu: (Student *) Stu//{//[_stu release];//_stu = [Stu retain];//}//the right way 4//both release and retain are placed within the IF statement block, or memory leaks are caused-(void) Setstu: (Student *) stu{if(_stu! =Stu) {[_stu release]; _stu=[Stu retain]; }}-(Student *) getstu{return_stu;}-(void) printf{NSLog (@"%@", _stu);}@end
3. Memory management of arrays
The memory management of arrays also uses the golden Rule of memory:
(1) When we create an array, the array will have a reference count of each object plus 1.
(2) When we destroy an array, the array will subtract 1 from each object reference count.
(3) When we add an object to an array, the object is referenced and counted plus 1.
(4) When we delete an object from an array, the reference count of the object is reduced by 1.
In summary, the golden rule of who creates who releases, abides by memory.
Test examples:
#import<Foundation/Foundation.h>#import "Student.h"intMainintargcConst Char*argv[]) {@autoreleasepool {//when an array is created: the memory address reference count that the object pointer points to plus 1//when an array reads an object: The reference count does not change. Student*STU1 =[[Student alloc] init]; Student*STU2 =[[Student alloc] init]; Nsarray*array =[[Nsarray alloc] initwithobjects:stu1,stu2, nil]; Student*STU3 = [Array Objectatindex:0]; NSLog (@"%ld", [stu1 Retaincount]); NSLog (@"%ld", [Stu3 Retaincount]); NSLog (@"%p", STU1); NSLog (@"%p", STU3); } return 0;}
Test results:
4. Automatic release
(1) The system automatically releases the pool: In the Runloop loop, the system determines that the system automatically releases the reference count of the objects within the pool, if 1 and holds object 1, the object is disposed, not 1 o'clock, if the object-1 is held, the reference count is 1, otherwise the reference count is unchanged.
(2) A custom auto-release pool that releases objects within the auto-free pool when the auto-free pool ends.
OC Base Memory Management