IOS記憶體釋放規則

來源:互聯網
上載者:User

  1. 當你使用 new、alloc 或 copy 建立對象時,對象的 count retain 到 1。你一定要負責把這個對象 release 或  autolease 掉。這樣當它的生命週期結束時,它才能清空。
    When you create an object using new,  alloc, or copy, the object has a retain count of 1. You are responsible  for sending the object a release or autorelease message when you’re done  with it. That way, it gets cleaned up when its useful life is over.
  2. 當你使用其他方法獲得一個對象時,你可以認為它已經 retain 了一個 count,並且 autolease 掉了。你不用考慮和它相關的清理問題。但是如果你想保留這個對象,那麼你需要 retain 它,並且要確保之後你 release 了這個對象。
    When you get hold of an  object via any other mechanism, assume it has a retain count of 1 and  that it has already been autoreleased. You don’t need to do any fur-  ther work to make sure it gets cleaned up. If you’re going to hang on to  the object for any
    length of time, retain it and make sure to release  it when you’re done.
  3. 如果你 retain 一個對象,你最終總是需要 release 或者 autolease 它。
    If you retain an object, you  need to (eventually) release or autorelease it. Balance these retains  and releases.

    這三條規則在寫代碼的時候一定要遵守,一旦遵守了一般也就不會有記憶體泄露的問題。

建立對象

    Objective-C 中建立對象分為 alloc 和 init 兩步,alloc 是在堆(heap)上初始化記憶體給物件變數,把變數(指標)設為 nil。每個類可以很多 init 方法,且每個方法都以 init 開頭,但每個類只有一個特定(designated)的 init 方法,NSObject 是 init;,UIView 是 - (id)initWithFrame:(CGRect)aRect;。在子類的 designated 方法中一定要調用父類的 designated 方法,子類其他的
init 方法只能調用子類自己的 designated 方法,不能調用父類的(即使用 self 而不是 super)。

下面是一些小知識點:

  1. 當你想暫時保留對象時,使用 autolease
            - (Money *)showMeTheMoney:(double)amount {
              Money *theMoney = [[Money alloc] init:amount];
              [theMoney autorelease];
              return theMoney;
            }
  2. 集合類的 autolease,一種方法是像對象一樣調用 autolease,另外也可以調用 [NSMutableArray array],最好的方式  return [NSArray arrayWithObjects:@“Steve”, @“Ankush”, @“Sean”,  nil];,其他類似的方法返回的對象都是 autolease 的對象。
        [NSString stringWithFormat:@“Meaning of %@ is %d”, @“life”, 42];
            [NSDictionary dictionaryWithObjectsAndKeys:ankush, @“TA”, janestudent, @“Student”, nil];
            [NSArray arrayWithContentsOfFile:(NSString *)path];
  3. 集合類對新增的對象擁有 ownership
  4. @"string" 是 autorelease 的
  5. NSString 一般是 copye 而不是 retain
  6. 你應該儘快 release 你擁有的對象,越快越好。建議建立完對象後就寫好 release 的代碼
  7. 當最後一個對象 owner release 後,會自動調用 dealloc 函數,在類中需要重載 dealloc,但永遠都不要自己去調用 dealloc
  8. @property 一般直接返回物件變數,我們可以把它理解為返回的是 autolease 的對象
  9. 使用 @synthesize 實現時,@property 可以指定 setter 函數使用 retain,copy 或 assign。assign 一般用在屬性一定會隨著對象的消亡而消亡的,比如 controller 的view,view 的 delegate
  10. Protocols 可以理解為抽象介面,delegat 和 dataSource 基本都是用 protocol 定義的

1. 通過分配或複製建立的對象保持計數1
2. 假設任何別的方法擷取的對象保持計數1,而且在自動釋放池中. 要想在當前執行範圍外使用該對象,就必須保持它
3. 向集合添加對象時它就被保持,從集合移除對象時就被釋放.釋放集合對象會釋放該集合中的所有對象
4. 確保有多少alloc,copy,mutableCopy或retain訊息就有多少release或autorelease訊息發送給該對象. 換句話說,確保你的代碼平衡
5. 在存取方法設定屬性,先保持,再釋放 (ztime: 現在有@propperty , @synthesize 兩個指令自動建立此代碼)
6. 用@"..."結構建立的NSString對象是常量.發送release或retain並無效果

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.