IOS基础 Day-1手动内存管理

辞职回家打算自学IOS开发,就在借个地方记录一下

Day-1      手动内存管理                  


主要内容:release  retain必须配对好,不然会占用内存 慢慢积累导致错误

一旦内存占用超过40m 45m时分别发生警告,一旦超过120m 系统将kill你的app 发生闪退

主要要防止发生的问题:
1.野指针操作
2.内存泄漏 
理解retain和assign的区别和 retain的原理
Main
  //
// main.m
// 1-1内存管理
//
// Created by k on 14-8-24.
// Copyright (c) 2014年 com.akleee.www. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Person.h"
#import "Car.h"
void withParam(Person *p)
{
[p retain];
NSLog(@"withParam : %@ ,retainCount : %lu",p.myName,(unsigned long)[p retainCount]);
[p release];
} void test1()
{
Person * p1=[[Person alloc ]init]; NSLog(@"retainCount = %lu", (unsigned long)[p1 retainCount]); p1.myName=@"haha"; NSLog(@"p1 : %@",p1); Person * p2 = [p1 retain]; NSLog(@"retainCount = %lu", (unsigned long)[p1 retainCount]); p2.myName=@"gaga"; NSLog(@"p1 : %@",p1); NSLog(@"p2 : %@",p2); withParam(p2); [p2 release]; [p1 release]; p1=nil; NSLog(@"%@",p1);//如果p1!=nil 则会发生内存泄露 NSLog(@"%lu", [p1 retainCount]);//nil 但是不报错
} void test2()
{
Car * c1=[[Car alloc ] init:@"haha"];//初始化 带参数 的对象 NSLog(@"%@",c1); [c1 release];
} void test3(){
Person * p1=[[Person alloc]init:@"ak"]; Car * c1= [[Car alloc]init:@"雪弗兰"]; [p1 setCar:c1]; [p1 run]; //1
// NSLog(@"p1 retainCount : %lu", [p1 retainCount]);
//1
//NSLog(@"c1 retainCount : %lu", [c1 retainCount]); [c1 release]; [p1 release];
} void test4()
{
//测试property的retain
Person * p1=[[Person alloc]init]; Car * c1=[[Car alloc]init]; Car * c2= [[Car alloc]init]; NSLog(@"c1:%lu",(unsigned long)[c1 retainCount]); p1.car=c1; NSLog(@"c1:%lu",(unsigned long)[c1 retainCount]); p1.car=c2; NSLog(@"c1:%lu",(unsigned long)[c1 retainCount]); NSLog(@"c2:%lu",(unsigned long)[c2 retainCount]); [c1 release];
[c2 release];
[p1 release];
} void test5()
{
Person * p1=[[Person alloc]init]; Car * c1=[[Car alloc]init]; Car * c2= [[Car alloc]init]; //
NSLog(@"c1:%lu",(unsigned long)[c1 retainCount]);
//
NSLog(@"c2:%lu",(unsigned long)[c2 retainCount]); [p1 setCar2:c1]; //
NSLog(@"c1:%lu",(unsigned long)[c1 retainCount]); [p1 setCar2:c2]; //
NSLog(@"c1:%lu",(unsigned long)[c1 retainCount]); //
NSLog(@"c2:%lu",(unsigned long)[c2 retainCount]); NSString * s1=@"aaa"; // NSString * s2=@"bbb"; NSLog(@"s1:%lu",(unsigned long)[s1 retainCount]); [p1 setMyName:s1]; NSLog(@"s1:%lu",(unsigned long)[s1 retainCount]); [c1 release];
[c2 release];
[p1 release]; }
int main(int argc, const char * argv[])
{ @autoreleasepool { // test1();
// test2();
// test3();
test4();
//test5();
}
return ;
}
 

Person.h

 //
// Person.h
// 1 基础知识
//
// Created by k on 14-8-24.
// Copyright (c) 2014年 com.akleee.www. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Car.h" @interface Person : NSObject
@property (nonatomic,assign , readwrite ) NSString * myName ;
@property (nonatomic,retain,readwrite) Car * car;
-(instancetype)init:(NSString *) Name;
-(void) speakMyName;
-(void)setCar:(Car *) car;
-(void) run; -(void)setCar2:(Car*)car2;
@end

person.m

 //
// Person.m
// 1 基础知识
//
// Created by k on 14-8-24.
// Copyright (c) 2014年 com.akleee.www. All rights reserved.
// #import "Person.h" @implementation Person
{
Car * _car2;
} - (instancetype)init:(NSString *) Name
{
self = [super init];
if (self) {
_myName=Name;
}
return self;
} -(void)speakMyName{ NSLog(@"大家好我的名字叫%@",_myName); } -(void)setCar2:(Car*)car2
{
if(_car2!=car2)
{
[_car2 release];
_car2=car2;
[car2 retain ];
}
} -(void)run{ NSLog(@"%@ 驾驶着 %@",_myName,_car.carName); } - (NSString *)description
{
return [NSString stringWithFormat:@"this is Person,myName=%@", _myName];
} - (void)dealloc
{
[_car2 release];
[super dealloc];
NSLog(@"Person对象被销毁");
}
@end

Car.h

 //
// Car.h
// 1 基础知识
//
// Created by k on 14-8-24.
// Copyright (c) 2014年 com.akleee.www. All rights reserved.
// #import <Foundation/Foundation.h> @interface Car : NSObject @property (nonatomic,assign,readwrite) NSString * carName;
- (instancetype)init:(NSString *)Name; -(void)run;
@end

Car.m

 //
// Car.m
// 1 基础知识
//
// Created by k on 14-8-24.
// Copyright (c) 2014年 com.akleee.www. All rights reserved.
// #import "Car.h" @implementation Car - (instancetype)init:(NSString *)Name
{
self = [super init];
if (self) {
_carName=Name;
}
return self;
}
-(void)run
{ NSLog(@"%@ is Running",_carName);
}
- (NSString *)description
{
return [NSString stringWithFormat:@"carName : %@", _carName];
}
- (void)dealloc
{
[super dealloc];
NSLog(@"Car %@ 被销毁",_carName);
}
@end
上一篇:Spring AOP AspectJ Pointcut Expressions With Examples--转


下一篇:Python 访问set