一、內存管理
RAM:運行時內存
ROM:存儲空間
管理內存的原因:
如果程序運行過程中不斷地創(chuàng)建對象,而不去管理對象的釋放,內存使用量會越來越大,在某一時刻會面臨不夠用的情況,最終導致程序崩潰。
二、OC管理內存的方式(引用計數(shù))
項目:MemoryManage0330
引用計數(shù):
1.ARC(Automatic Reference Counting自動引用計數(shù))
2.MRC(Manual Reference Counting手動引用計數(shù))
OC中的對象有一個“引用計數(shù)”屬性,引用計數(shù)表示當前有多少個其他對象正在使用該對象,當一個對象引用計數(shù)由1變?yōu)?時,該對象就會被釋放。
1.使用MRC前,先關閉ARC
內存管理不當,會出現(xiàn)的兩種現(xiàn)象:
內存泄漏:本來該被釋放的對象,沒有被釋放,一直占用內存空間。
過度釋放:對象在不該釋放時被釋放,造成程序崩潰。
引用計數(shù)為0時,就會執(zhí)行類內部的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.凡是通過alloc 或 new創(chuàng)建的對象,對象引用計數(shù)為1。
People *p1 = [[People alloc]init];
NSLog(@"p1 = %d",p1.retainCount);
[p1 play];
//讓引用計數(shù)減一(并不是釋放)
//現(xiàn)在p1.retainCount == 0
// [p1 release];
//new == alloc + init
People *p2 = [People new];
NSLog(@"p2 = %d",p2.retainCount);
People *p3 = p1;
//讓引用計數(shù)加一
[p3 retain];//等價于[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
//引用計數(shù)為0時,就會執(zhí)行類內部的dealloc方法
- (void)dealloc
{
NSLog(@"%s",__FUNCTION__);
[super dealloc];
}
- (void)play
{
NSLog(@"玩");
}
@end
三、全局變量的內存管理
項目:MemoryManage_GlobalVariable0330
四、button/Label等控件的內存管理
項目:MemoryManage_UIButtonLabel0330
創(chuàng)建對象:alloc、new
引用計數(shù)+1:retain、addSubView、addObject
引用計數(shù)-1:autorelease、removeObject
注意:addSubView、addObject不需要程序員管理
autorelease:自動釋放
事件循環(huán):待命狀態(tài)→接收事件→執(zhí)行完畢→待命狀態(tài)(程序從待命狀態(tài) 到 接收一個事件 執(zhí)行一段代碼 執(zhí)行完畢重新回到待命狀態(tài)的過程)
在一個有返回值的(+/-)方法(如UIButton的buttonWithType方法)中,alloc/new 出來的對象需要return出去繼續(xù)使用,那么這個對象就調用autorelease自動管理內存。
調用autorelease的對象會被放入自動釋放池,在一次“事件循環(huán)”結束之后,池會進行排干操作,池中對象的引用計數(shù)就會-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];
//誰創(chuàng)建(alloc),誰管理(release);誰引用(retain),誰管理(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 能使對象引用計數(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];
//對象添加到數(shù)組/字典 中,對象引用計數(shù)+1
NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:p1, nil];
NSLog(@"p1 = %d",p1.retainCount);
//對象從數(shù)組/字典 中移除時,對象引用計數(shù)-1
[arr removeObject:p1];
NSLog(@"p1 = %d",p1.retainCount);
[p1 dealloc];
//當數(shù)組/字典 的引用計數(shù)為0時,數(shù)組/字典 中元素的引用計數(shù)會-1
[arr release];
//這個button不是alloc出來的,所以不用程序員自己管理*************************
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:button];
NSLog(@"button = %d",button.retainCount);
}
//在一個方法(+/-)中,alloc/new 出來的對象,需要作為返回值返回出去繼續(xù)使用,那么這個對象就調用autorelease,管理內存。
//例如下面的createImageView方法,
//或UIButton的buttonWithType方法。
+ (UIImageView *)createImageView
{
UIImageView *imgView = [[UIImageView alloc]init];
//autorelease:自動釋放
//如果對象調用autorelease,該對象會被放入自動釋放池中,
//當一次事件循環(huán)結束之后,自動釋放池會進行排干,此時,自動釋放池中的對象的引用計數(shù)就會-1.
//事件循環(huán):表示程序,從待命狀態(tài)到接收一個事件并觸發(fā)執(zhí)行一段代碼,執(zhí)行完畢重新回到待命狀態(tài)的過程。
[imgView autorelease];
return imgView;
}
Peopler.m
#import "People.h"
@implementation People
- (void)dealloc
{
NSLog(@"%s",__func__);
[super dealloc];
}
@end
五、屬性的與set方法
項目:MemoryManage_setMethod_retain_assign0330
六、代理內存管理
項目:MemoryManage_Delegate0330
作業(yè):100生活關閉ARC
使用MRC