OC基礎(chǔ)學(xué)習(xí)7:內(nèi)存管理

1 對(duì)象生命周期

誕生(通過(guò)alloc或new方法實(shí)現(xiàn)) -> 生存(接收消息并執(zhí)行操作) -> 交友(通過(guò)復(fù)合以及向方法傳遞參數(shù)) -> 死去(被釋放掉)

引用計(jì)數(shù)(reference counting) / 保留計(jì)數(shù)(retain counting)

  • alloc, new, copy: 創(chuàng)建對(duì)象,保留計(jì)數(shù)被設(shè)置為1
  • retain: 保留計(jì)數(shù)加1。如[[car retain] setTire: tire atIndex:2];表示要求car對(duì)象將其保留計(jì)數(shù)的值加1并執(zhí)行setTire操作。
  • release: 保留計(jì)數(shù)減1
  • dealloc: 保留計(jì)數(shù)歸0時(shí)自動(dòng)調(diào)用
  • retainCount: 獲得對(duì)象保留計(jì)數(shù)當(dāng)前值
#import <Foundation/Foundation.h>

@interface RetainTracker : NSObject

@end
@implementation RetainTracker

- (id) init
{
    if (self = [super init]) {
        NSLog(@"init: Retain count of %lu .", [self retainCount]);
    }
    return self;
} // init
- (void)dealloc
{
    NSLog(@"dealloc called. Bye Bye.");
    [super dealloc];
} // dealloc

@end // RetainTracker



int main(int argc, const char * argv[]) {
    RetainTracker *a = [RetainTracker new];
    
    [a retain];
    NSLog(@"%lu", [a retainCount]);
    
    [a retain];
    NSLog(@"%lu", [a retainCount]);
    
    [a release];
    NSLog(@"%lu", [a retainCount]);
    
    [a release];
    NSLog(@"%lu", [a retainCount]);
    
    [a release];
    NSLog(@"%lu", [a retainCount]);  // 為什么此處是  1 不是 0嗎?
    
    return 0;
}

對(duì)象所有權(quán)(object ownership)

如果一個(gè)對(duì)象內(nèi)有指向其他對(duì)象的實(shí)例變量,則稱 該對(duì)象擁有這些對(duì)象。

訪問(wèn)方法中的保留和釋放

所有對(duì)象放入池中

  • @autoreleasepool/NSAutoreleasePool: 自動(dòng)釋放池
  • NSObject提供autorelease方法:
    - (id) autorelease;

2 Cocoa的內(nèi)存管理規(guī)則

內(nèi)存管理規(guī)則

3 異常

與異常有關(guān)的關(guān)鍵字

  • @try
  • @catch
  • @finally
  • @throw

捕捉不同類型的異常

@try {
} @catch (MyCustomException *custom) {
} @catch (NSException *exception) {
} @catch (id value) {
} @finally {
}

拋出異常

拋出異常的兩種方式:

  • @throw異常名
  • 向某個(gè)NSException對(duì)象發(fā)送raise消息
NSException *theException = [NSException exceptionWithName: ...];

@throw theException;
[theException raise];

區(qū)別:raise只對(duì)NSException對(duì)象有效,@throw異常名可用在其他對(duì)象上。

異常也需要內(nèi)存管理

異常和自動(dòng)釋放池

最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 導(dǎo)讀 一、為什么要進(jìn)行內(nèi)存管理 二、內(nèi)存管理機(jī)制 三、內(nèi)存管理原則 四、MRC手動(dòng)內(nèi)存管理 五、ARC自動(dòng)內(nèi)存管理...
    千山小畻閱讀 2,291評(píng)論 0 7
  • 內(nèi)存管理的基本范圍和概念. 程序運(yùn)行過(guò)程中藥創(chuàng)建大量的對(duì)象, 和其他高級(jí)語(yǔ)言類似,在ObjC中對(duì)象存儲(chǔ)在堆區(qū),程序...
    ValienZh閱讀 966評(píng)論 0 2
  • 29.理解引用計(jì)數(shù) Objective-C語(yǔ)言使用引用計(jì)數(shù)來(lái)管理內(nèi)存,也就是說(shuō),每個(gè)對(duì)象都有個(gè)可以遞增或遞減的計(jì)數(shù)...
    Code_Ninja閱讀 1,749評(píng)論 1 3
  • 今天看到一篇不錯(cuò)的文章關(guān)于OC內(nèi)存管理的,轉(zhuǎn)載一下與你共享概述我們知道在程序運(yùn)行過(guò)程中要?jiǎng)?chuàng)建大量的對(duì)象,和其他高級(jí)...
    niceSYT閱讀 518評(píng)論 0 2
  • 2017年2月28日 星期二 晴 【早起】5:00起床。 【學(xué)習(xí)】1.親子共讀:《詩(shī)經(jīng)》29-31...
    滋心潤(rùn)霖閱讀 661評(píng)論 0 0

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