zoukankan      html  css  js  c++  java
  • iOS Memory Management

     Memory management in a Cocoa application that doesn’t use garbage collection is based on a reference counting model. When you create or copy an object, its retain count is 1. Thereafter other objects may express an ownership interest in your object, which increments its retain count. The owners of an object may also relinquish their ownership interest in it, which decrements the retain count. When the retain count becomes zero, the object is deallocated (destroyed).

    To assist you in memory management, Objective-C gives you methods and mechanisms that you must use in conformance with a set of rules.

    Memory management

    Memory-Management Rules

    Memory-management rules, sometimes referred to as the ownership policy, help you to explicitly manage memory in Objective-C code.

    • You own any object you create by allocating memory for it or copying it.

      Related methods: allocallocWithZone:copycopyWithZone:mutableCopy,mutableCopyWithZone:

    • If you are not the creator of an object, but want to ensure it stays in memory for you to use, you can express an ownership interest in it.

      Related method: retain

    • If you own an object, either by creating it or expressing an ownership interest, you are responsible for releasing it when you no longer need it.

      Related methods: releaseautorelease

    • Conversely, if you are not the creator of an object and have not expressed an ownership interest, you mustnot release it.

    If you receive an object from elsewhere in your program, it is normally guaranteed to remain valid within the method or function it was received in. If you want it to remain valid beyond that scope, you should retain or copy it. If you try to release an object that has already been deallocated, your program crashes.

    Aspects of Memory Management

    The following concepts are essential to understanding and properly managing object memory:

    • Autorelease pools. Sending autorelease to an object marks the object for later release, which is useful when you want the released object to persist beyond the current scope. Autoreleasing an object puts it in an autorelease pool (an instance of NSAutoreleasePool), which is created for an arbitrary program scope. When program execution exits that scope, the objects in the pool are released.

    • Deallocation. When an object’s retain count drops to zero, the runtime calls the dealloc method of the object’s class just before it destroys the object. A class implements this method to free any resources the object holds, including objects pointed to by its instance variables.

    • Factory methods. Many framework classes define class methods that, as a convenience, create objects of the class for you. These returned objects are not guaranteed to be valid beyond the receiving method’s scope.

  • 相关阅读:
    分而治之应该把握哪些原则呢
    什么事SOA
    SOA架构设计的案例分析课后 学习
    关于分层架构的知识学习
    从小编程,到架构师,我们应该具备什么
    基于MVC架构实例分析以校园知网校内论坛功能为例
    服务架构设计及其应用
    《一线架构师实践指南》阅读笔记03
    《一线架构师实践指南》阅读笔记02
    Pre-Architecture 阶段阅读笔记
  • 原文地址:https://www.cnblogs.com/alexfan/p/2091116.html
Copyright © 2011-2022 走看看