iOS Memory Management Basics

To keep memory consumption as low as possible in an application, it is necessary to get rid of objects that are not being used, but you need to make sure that you don’t get rid of an object that is being used. You therefore need a mechanism that allows you to mark an object as still being useful. In many respects, memory management is thus best understood in terms of “object ownership.”
  • An object may have one or more owners.
    By way of an analogy, consider a timeshare apartment.
  • When an object has no owners, it is destroyed.
    To stretch the analogy, consider a timeshare complex that is not loved by the local population. If there are no owners, the complex will be torn down.
  • To make sure an object you’re interested in is not destroyed, you must become an owner.
    You can either build a new apartment, or take a stake in an existing one.
  • To allow an object in which you’re no longer interested to be destroyed, you relinquish ownership.
    You can sell your timeshare apartment.
To support this model, Cocoa provides a mechanism called “reference counting” or “retain counting.” Every object has a retain count. An object is created with a retain count of 1. When the retain count drops to 0, an object is deallocated (destroyed). You manipulate the retain count (take and relinquish ownership) using a variety of methods:
alloc
Allocates memory for an object, and returns it with retain count of 1. You own objects you create using any method that starts with the word alloc or with the word new.
copy
Makes a copy of an object, and returns it with retain count of 1. If you copy an object, you own the copy. This applies to any method that contains the word copy where “copy” refers to the object being returned.
retain
Increases the retain count of an object by 1. Takes ownership of an object.
release
Decreases the retain count of an object by 1. Relinquishes ownership of an object.
autorelease
Decreases the reference count of an object by 1 at some stage in the future. Relinquishes ownership of an object at some stage in the future.
The practical rules for memory management are as follows :
  • You only own objects you created using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message.
    Many classes provide methods of the form +className... that you can use to obtain a new instance of the class. Often referred to as “convenience constructors”, these methods create a new instance of the class, initialize it, and return it for you to use. You do not own objects returned from convenience constructors, or from other accessor methods.
  • Once you have finished with an object that you own, you should relinquish ownership using release or autorelease.
    Typically you should use release rather than autorelease. You use autorelease only when immediate deallocation of the object would be inappropriate, for example you are returning an object from a method. (Note: this does not imply that release necessarily results in deallocation of an object—it will only do so if the retain count drops to 0 as a result
  • Implement a dealloc method to release the instance variables you own.
  • You should never invoke dealloc directly (other than when you invoke super’s implementation in a custom dealloc method).

    IT IS IMPORTANT TO GO THROUGH THE BASICS
Follow mohit2305 on Twitter

Comments

Post a Comment