Object-C記憶體管理-對象引用計數的特例

來源:互聯網
上載者:User

標籤:style   java   sp   strong   on   問題   log   代碼   bs   

看到OC中記憶體管理這塊,其中的引用計數部分,由部分10.5上的EBOOK樣本已經在10.9上不能運行正確了,比如下面的代碼:

 NSString * str1 = @"string 1";

    NSString * str2 = @"string 2";

    NSMutableString * mstr3 = [NSMutableString stringWithString: @"string 3"];

    NSNumber * int1 = [NSNumber numberWithInteger: 1800000];

    NSNumber * long1 = [NSNumber numberWithLong: 1800000];

    NSNumber * double1 = [NSNumber numberWithDouble: 300.0];

    

    NSLog(@"retain count: str1=%lx, str2=%lx, str3=%lx, int1=%lx, long1=%lx, double1=%lx",

          [str1 retainCount],

          [str2 retainCount],

          [mstr3 retainCount],

          [int1 retainCount],

          [long1 retainCount],

          [double1 retainCount]);

運行結果是:

014-11-07 17:05:22.128 Demo17[9620:303] retain count: str1=ffffffffffffffff, str2=ffffffffffffffff, str3=1, int1=7fffffffffffffff, long1=7fffffffffffffff, double1=1

Program ended with exit code: 0

EBOOK上講的是常量字串對象不參與計數,這個是正確的,但是實際運行結果發現,整形的NSNumber對象也好像不參與計數,浮點的NSNumber對象參與計數,即使整形數字很大也不參與計數。Java裡面曾經由實現是對小額數字封裝對象做緩衝,所有小額數字封裝對象不參與計數。但是OC裡面看起來不是這樣的,因為數字已經足夠大,不能緩衝這麼多的整形對象吧?

OC的具體是什麼後面再繼續研究。

目前總結出的結論是:

1. 常量字串不參與計數。

2. 常量整形NSNumber對象不參與計數。

3. 其餘的對象參與計數。

 

打完收工,有問題再記錄~

Object-C記憶體管理-對象引用計數的特例

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.