IOS基础 Day-1手动内存管理
辞职回家打算自学IOS开发,就在借个地方记录一下
Day-1 手动内存管理
主要内容:release retain必须配对好,不然会占用内存 慢慢积累导致错误
一旦内存占用超过40m 45m时分别发生警告,一旦超过120m 系统将kill你的app 发生闪退
主要要防止发生的问题:
1.野指针操作
2.内存泄漏
理解retain和assign的区别和 retain的原理
Main
1 // 2 // main.m 3 // 1-1内存管理 4 // 5 // Created by k on 14-8-24. 6 // Copyright (c) 2014年 com.akleee.www. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 #import "Person.h" 11 #import "Car.h" 12 void withParam(Person *p) 13 { 14 [p retain]; 15 NSLog(@"withParam : %@ ,retainCount : %lu",p.myName,(unsigned long)[p retainCount]); 16 [p release]; 17 } 18 19 void test1() 20 { 21 Person * p1=[[Person alloc ]init]; 22 23 NSLog(@"retainCount = %lu", (unsigned long)[p1 retainCount]); 24 25 26 p1.myName=@"haha"; 27 28 NSLog(@"p1 : %@",p1); 29 30 Person * p2 = [p1 retain]; 31 32 NSLog(@"retainCount = %lu", (unsigned long)[p1 retainCount]); 33 34 35 p2.myName=@"gaga"; 36 37 NSLog(@"p1 : %@",p1); 38 39 NSLog(@"p2 : %@",p2); 40 41 withParam(p2); 42 43 44 [p2 release]; 45 46 [p1 release]; 47 48 p1=nil; 49 50 NSLog(@"%@",p1);//如果p1!=nil 则会发生内存泄露 51 52 NSLog(@"%lu", [p1 retainCount]);//nil 但是不报错 53 } 54 55 void test2() 56 { 57 Car * c1=[[Car alloc ] init:@"haha"];//初始化 带参数 的对象 58 59 NSLog(@"%@",c1); 60 61 [c1 release]; 62 } 63 64 void test3(){ 65 Person * p1=[[Person alloc]init:@"ak"]; 66 67 Car * c1= [[Car alloc]init:@"雪弗兰"]; 68 69 [p1 setCar:c1]; 70 71 [p1 run]; 72 73 //1 74 // NSLog(@"p1 retainCount : %lu", [p1 retainCount]); 75 //1 76 //NSLog(@"c1 retainCount : %lu", [c1 retainCount]); 77 78 [c1 release]; 79 80 [p1 release]; 81 } 82 83 void test4() 84 { 85 //测试property的retain 86 Person * p1=[[Person alloc]init]; 87 88 Car * c1=[[Car alloc]init]; 89 90 Car * c2= [[Car alloc]init]; 91 92 NSLog(@"c1:%lu",(unsigned long)[c1 retainCount]); 93 94 95 p1.car=c1; 96 97 98 NSLog(@"c1:%lu",(unsigned long)[c1 retainCount]); 99 100 101 p1.car=c2; 102 103 NSLog(@"c1:%lu",(unsigned long)[c1 retainCount]); 104 105 NSLog(@"c2:%lu",(unsigned long)[c2 retainCount]); 106 107 [c1 release]; 108 [c2 release]; 109 [p1 release]; 110 } 111 112 void test5() 113 { 114 Person * p1=[[Person alloc]init]; 115 116 Car * c1=[[Car alloc]init]; 117 118 Car * c2= [[Car alloc]init]; 119 120 //1 121 NSLog(@"c1:%lu",(unsigned long)[c1 retainCount]); 122 //1 123 NSLog(@"c2:%lu",(unsigned long)[c2 retainCount]); 124 125 126 127 [p1 setCar2:c1]; 128 129 //2 130 NSLog(@"c1:%lu",(unsigned long)[c1 retainCount]); 131 132 133 [p1 setCar2:c2]; 134 135 //1 136 NSLog(@"c1:%lu",(unsigned long)[c1 retainCount]); 137 138 //2 139 NSLog(@"c2:%lu",(unsigned long)[c2 retainCount]); 140 141 142 143 NSString * s1=@"aaa"; 144 145 // NSString * s2=@"bbb"; 146 147 NSLog(@"s1:%lu",(unsigned long)[s1 retainCount]); 148 149 [p1 setMyName:s1]; 150 151 NSLog(@"s1:%lu",(unsigned long)[s1 retainCount]); 152 153 [c1 release]; 154 [c2 release]; 155 [p1 release]; 156 157 } 158 int main(int argc, const char * argv[]) 159 { 160 161 @autoreleasepool { 162 163 // test1(); 164 // test2(); 165 // test3(); 166 test4(); 167 //test5(); 168 } 169 return 0; 170 }
Person.h
1 // 2 // Person.h 3 // 1 基础知识 4 // 5 // Created by k on 14-8-24. 6 // Copyright (c) 2014年 com.akleee.www. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 #import "Car.h" 11 12 @interface Person : NSObject 13 @property (nonatomic,assign , readwrite ) NSString * myName ; 14 @property (nonatomic,retain,readwrite) Car * car; 15 -(instancetype)init:(NSString *) Name; 16 -(void) speakMyName; 17 -(void)setCar:(Car *) car; 18 -(void) run; 19 20 21 22 -(void)setCar2:(Car*)car2; 23 @end 24
person.m
1 // 2 // Person.m 3 // 1 基础知识 4 // 5 // Created by k on 14-8-24. 6 // Copyright (c) 2014年 com.akleee.www. All rights reserved. 7 // 8 9 #import "Person.h" 10 11 @implementation Person 12 { 13 Car * _car2; 14 } 15 16 - (instancetype)init:(NSString *) Name 17 { 18 self = [super init]; 19 if (self) { 20 _myName=Name; 21 } 22 return self; 23 } 24 25 -(void)speakMyName{ 26 27 NSLog(@"大家好我的名字叫%@",_myName); 28 29 } 30 31 32 33 -(void)setCar2:(Car*)car2 34 { 35 if(_car2!=car2) 36 { 37 [_car2 release]; 38 _car2=car2; 39 [car2 retain ]; 40 } 41 } 42 43 -(void)run{ 44 45 NSLog(@"%@ 驾驶着 %@",_myName,_car.carName); 46 47 } 48 49 - (NSString *)description 50 { 51 return [NSString stringWithFormat:@"this is Person,myName=%@", _myName]; 52 } 53 54 - (void)dealloc 55 { 56 [_car2 release]; 57 [super dealloc]; 58 NSLog(@"Person对象被销毁"); 59 } 60 @end
Car.h
1 // 2 // Car.h 3 // 1 基础知识 4 // 5 // Created by k on 14-8-24. 6 // Copyright (c) 2014年 com.akleee.www. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface Car : NSObject 12 13 @property (nonatomic,assign,readwrite) NSString * carName; 14 - (instancetype)init:(NSString *)Name; 15 16 -(void)run; 17 @end
Car.m
1 // 2 // Car.m 3 // 1 基础知识 4 // 5 // Created by k on 14-8-24. 6 // Copyright (c) 2014年 com.akleee.www. All rights reserved. 7 // 8 9 #import "Car.h" 10 11 @implementation Car 12 13 - (instancetype)init:(NSString *)Name 14 { 15 self = [super init]; 16 if (self) { 17 _carName=Name; 18 } 19 return self; 20 } 21 -(void)run 22 { 23 24 NSLog(@"%@ is Running",_carName); 25 } 26 - (NSString *)description 27 { 28 return [NSString stringWithFormat:@"carName : %@", _carName]; 29 } 30 - (void)dealloc 31 { 32 [super dealloc]; 33 NSLog(@"Car %@ 被销毁",_carName); 34 } 35 @end