01引用計(jì)數(shù)_全局變量_delegate_button/Label等控件的內(nèi)存管理_retain/assign

一、內(nèi)存管理
RAM:運(yùn)行時(shí)內(nèi)存
ROM:存儲(chǔ)空間
管理內(nèi)存的原因:
如果程序運(yùn)行過程中不斷地創(chuàng)建對(duì)象,而不去管理對(duì)象的釋放,內(nèi)存使用量會(huì)越來越大,在某一時(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)存泄漏:本來該被釋放的對(duì)象,沒有被釋放,一直占用內(nèi)存空間。
過度釋放:對(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.凡是通過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)的過程)
在一個(gè)有返回值的(+/-)方法(如UIButton的buttonWithType方法)中,alloc/new 出來的對(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];
    //誰創(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 能使對(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出來的,所以不用程序員自己管理*************************
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button];
    NSLog(@"button = %d",button.retainCount);
    
}
//在一個(gè)方法(+/-)中,alloc/new 出來的對(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)的過程。
    [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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.1 什么是自動(dòng)引用計(jì)數(shù) 概念:在 LLVM 編譯器中設(shè)置 ARC(Automaitc Reference Co...
    __silhouette閱讀 5,469評(píng)論 1 17
  • 內(nèi)存管理 簡(jiǎn)述OC中內(nèi)存管理機(jī)制。與retain配對(duì)使用的方法是dealloc還是release,為什么?需要與a...
    丶逐漸閱讀 2,081評(píng)論 1 16
  • 前言 之前的兩篇拙文C語言-內(nèi)存管理基礎(chǔ)、C語言-內(nèi)存管理深入 介紹了關(guān)于C語言在內(nèi)存管理方面的相關(guān)知識(shí)。但是對(duì)于...
    老板娘來盤一血閱讀 3,817評(píng)論 25 35
  • 29.理解引用計(jì)數(shù) Objective-C語言使用引用計(jì)數(shù)來管理內(nèi)存,也就是說,每個(gè)對(duì)象都有個(gè)可以遞增或遞減的計(jì)數(shù)...
    Code_Ninja閱讀 1,730評(píng)論 1 3
  • 1. 內(nèi)總管理原則(引用計(jì)數(shù)) IOS的對(duì)象都繼承于NSObject, 該對(duì)象有一個(gè)方法:retainCount...
    lilinjianshu閱讀 2,242評(píng)論 0 2

友情鏈接更多精彩內(nèi)容