一、內(nèi)存管理
RAM:運(yùn)行時(shí)內(nèi)存
ROM:存儲(chǔ)空間
管理內(nèi)存的原因:
如果程序運(yùn)行過(guò)程中不斷地創(chuàng)建對(duì)象,而不去管理對(duì)象的釋放,內(nèi)存使用量會(huì)越來(lái)越大瓮顽,在某一時(shí)刻會(huì)面臨不夠用的情況,最終導(dǎo)致程序崩潰围橡。
二暖混、OC管理內(nèi)存的方式(引用計(jì)數(shù))
項(xiàng)目:MemoryManage0330
引用計(jì)數(shù):
1.ARC(Automatic Reference Counting自動(dòng)引用計(jì)數(shù))
2.MRC(Manual Reference Counting手動(dòng)引用計(jì)數(shù))
OC中的對(duì)象有一個(gè)“引用計(jì)數(shù)”屬性,引用計(jì)數(shù)表示當(dāng)前有多少個(gè)其他對(duì)象正在使用該對(duì)象某饰,當(dāng)一個(gè)對(duì)象引用計(jì)數(shù)由1變?yōu)?時(shí)儒恋,該對(duì)象就會(huì)被釋放。
1.使用MRC前黔漂,先關(guān)閉ARC
內(nèi)存管理不當(dāng)诫尽,會(huì)出現(xiàn)的兩種現(xiàn)象:
內(nèi)存泄漏:本來(lái)該被釋放的對(duì)象,沒(méi)有被釋放炬守,一直占用內(nèi)存空間牧嫉。
過(guò)度釋放:對(duì)象在不該釋放時(shí)被釋放,造成程序崩潰。
引用計(jì)數(shù)為0時(shí)酣藻,就會(huì)執(zhí)行類內(nèi)部的dealloc方法
全局變量的釋放:在類的dealloc方法中釋放
@interface ViewController1 ()
{
People *_p1;
}
@end
@implementation ViewController1
//注意:全局變量在dealloc中釋放
//全局變量的釋放方法:先釋放子類块促,再釋放父類
- (void)dealloc
{
[_p1 release];
NSLog(@"%s",__func__);
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
_p1 = [[People alloc]init];
}
源碼:
文件:MemoryManage0330/ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
//1.凡是通過(guò)alloc 或 new創(chuàng)建的對(duì)象龄句,對(duì)象引用計(jì)數(shù)為1。
People *p1 = [[People alloc]init];
NSLog(@"p1 = %d",p1.retainCount);
[p1 play];
//讓引用計(jì)數(shù)減一(并不是釋放)
//現(xiàn)在p1.retainCount == 0
// [p1 release];
//new == alloc + init
People *p2 = [People new];
NSLog(@"p2 = %d",p2.retainCount);
People *p3 = p1;
//讓引用計(jì)數(shù)加一
[p3 retain];//等價(jià)于[p1 retain]
NSLog(@"release前————————————");
NSLog(@"p1 = %d",p1.retainCount);
NSLog(@"p3 = %d",p3.retainCount);
NSLog(@"release后————————————");
NSLog(@"p1 = %d",p1.retainCount);
NSLog(@"p3 = %d",p3.retainCount);
//注意:局部變量 往往在方法體末尾 release
[p1 release];
[p2 release];
[p3 release];
}
Peopler.m
@implementation People
//引用計(jì)數(shù)為0時(shí),就會(huì)執(zhí)行類內(nèi)部的dealloc方法
- (void)dealloc
{
NSLog(@"%s",__FUNCTION__);
[super dealloc];
}
- (void)play
{
NSLog(@"玩");
}
@end
三屎鳍、全局變量的內(nèi)存管理
項(xiàng)目:MemoryManage_GlobalVariable0330
四凭涂、button/Label等控件的內(nèi)存管理
項(xiàng)目:MemoryManage_UIButtonLabel0330
創(chuàng)建對(duì)象:alloc断傲、new
引用計(jì)數(shù)+1:retain败徊、addSubView、addObject
引用計(jì)數(shù)-1:autorelease撞羽、removeObject
注意:addSubView阐斜、addObject不需要程序員管理
autorelease:自動(dòng)釋放
事件循環(huán):待命狀態(tài)→接收事件→執(zhí)行完畢→待命狀態(tài)(程序從待命狀態(tài) 到 接收一個(gè)事件 執(zhí)行一段代碼 執(zhí)行完畢重新回到待命狀態(tài)的過(guò)程)
在一個(gè)有返回值的(+/-)方法(如UIButton的buttonWithType方法)中,alloc/new 出來(lái)的對(duì)象需要return出去繼續(xù)使用诀紊,那么這個(gè)對(duì)象就調(diào)用autorelease自動(dòng)管理內(nèi)存谒出。
調(diào)用autorelease的對(duì)象會(huì)被放入自動(dòng)釋放池,在一次“事件循環(huán)”結(jié)束之后邻奠,池會(huì)進(jìn)行排干操作笤喳,池中對(duì)象的引用計(jì)數(shù)就會(huì)-1。
源碼:
Appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//誰(shuí)創(chuàng)建(alloc)惕澎,誰(shuí)管理(release)莉测;誰(shuí)引用(retain),誰(shuí)管理(release)
ViewController1 *vc1 = [[ViewController1 alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc1];
// NSLog(@"vc1 ----%d",vc1.retainCount);
self.window.rootViewController = nav;
// NSLog(@"nav ----%d",nav.retainCount);
[vc1 release];
[nav release];
return YES;
}
ViewController1.m
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *label = [[UILabel alloc]init];
NSLog(@"label = %d",label.retainCount);
//addSubview 能使對(duì)象引用計(jì)數(shù)+1
[self.view addSubview:label];
NSLog(@"label = %d",label.retainCount);
_view1 = [[UIView alloc]init];
[self.view addSubview:_view1];
NSLog(@"view1 = %d",_view1.retainCount);
//使用數(shù)組add remove
People *p1 = [[People alloc]init];
//對(duì)象添加到數(shù)組/字典 中唧喉,對(duì)象引用計(jì)數(shù)+1
NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:p1, nil];
NSLog(@"p1 = %d",p1.retainCount);
//對(duì)象從數(shù)組/字典 中移除時(shí),對(duì)象引用計(jì)數(shù)-1
[arr removeObject:p1];
NSLog(@"p1 = %d",p1.retainCount);
[p1 dealloc];
//當(dāng)數(shù)組/字典 的引用計(jì)數(shù)為0時(shí)忍抽,數(shù)組/字典 中元素的引用計(jì)數(shù)會(huì)-1
[arr release];
//這個(gè)button不是alloc出來(lái)的八孝,所以不用程序員自己管理*************************
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:button];
NSLog(@"button = %d",button.retainCount);
}
//在一個(gè)方法(+/-)中,alloc/new 出來(lái)的對(duì)象鸠项,需要作為返回值返回出去繼續(xù)使用干跛,那么這個(gè)對(duì)象就調(diào)用autorelease,管理內(nèi)存祟绊。
//例如下面的createImageView方法楼入,
//或UIButton的buttonWithType方法。
+ (UIImageView *)createImageView
{
UIImageView *imgView = [[UIImageView alloc]init];
//autorelease:自動(dòng)釋放
//如果對(duì)象調(diào)用autorelease牧抽,該對(duì)象會(huì)被放入自動(dòng)釋放池中嘉熊,
//當(dāng)一次事件循環(huán)結(jié)束之后,自動(dòng)釋放池會(huì)進(jìn)行排干扬舒,此時(shí)阐肤,自動(dòng)釋放池中的對(duì)象的引用計(jì)數(shù)就會(huì)-1.
//事件循環(huán):表示程序,從待命狀態(tài)到接收一個(gè)事件并觸發(fā)執(zhí)行一段代碼,執(zhí)行完畢重新回到待命狀態(tài)的過(guò)程孕惜。
[imgView autorelease];
return imgView;
}
Peopler.m
#import "People.h"
@implementation People
- (void)dealloc
{
NSLog(@"%s",__func__);
[super dealloc];
}
@end
五愧薛、屬性的與set方法
項(xiàng)目:MemoryManage_setMethod_retain_assign0330
六、代理內(nèi)存管理
項(xiàng)目:MemoryManage_Delegate0330
作業(yè):100生活關(guān)閉ARC
使用MRC