[Reprint] A little summary and understanding of memory management in iOS development

Source: Internet
Author: User

a summary and understanding of memory management in iOS development

iOS development has been for two years, feel the need to sink into some things, especially some basic things, although now has arc this kind of thing, but I have not been used, personally feel that the understanding of memory operations is a measure of the maturity of a programmer is a standard. Well, gossip does not say, below to get to the point. As we all know, Objectivec's memory management refers to a method called "Reference count" (Reference count), the simple understanding is the system for each created object, (here to note, just the object, NSObject subclass, the basic type does not ' Reference count ') records a reference count, initializes the object when the Alloc method is called, the system in the Alloc method will be the object's reference count +1; For example: UIView *myview = [UIView alloc] init]; The object created here MyView its current reference technology is 1. All objects can call [xxx reatin]; make a reference count of +1. You can also call [XXX release]; make a reference count of itself-1. If an object has a reference count of 0, the object is immediately (remember, immediately) reclaimed by the system. In addition to [xxx reatin]; The object's reference count is +1. There are a number of methods that will refer to the object's reference technique + 1, with two most commonly used: [Self.view Addsubview:myview]; This method will count the MyView reference to +1. [Array Addobject:myview] in the Nsarray class; The reference count is also +1. I'll use the code to illustrate some of the more error-prone areas.

(i) Description of the reference count for the temporary variable object

UIView *v = [[UIView alloc] init]; Post-allocation reference count is 1

[OBJC]View Plaincopy
    1. [self. View addsubview:v]; //Here the reference count plus 1, is 2
    2. [v release]; //Here the reference count is-1 is 1
    3. Finally, when the system recycles self . View, it recycles its subview, so self . When the view is reclaimed, the reference count of V is 0,v and is immediately recycled.
(ii) Description of the reference count of the member variable object[OBJC]View Plaincopy
    1. v = [[UIView alloc] init];
    2. [self. View addsubview:v];
    3. [v release];
    4. If [v release] is called in the Dealloc , then more release will be crash.
(c) If V is a property of a class and is declared, it is Assgin[OBJC]View Plaincopy
  1. @property (nonatomic, assign) UIView *v; Here is assign, and then when allocating memory, if that's the case,
  2. Self. v = [[UIView alloc] init]; Count is 1
  3. [self. View addsubview:self. v]; Count is 2
  4. [self. V release]; Count is 1
  5. You do not need to be in the Dealloc [self. v release]; Because the object reference count of assign declaration is not automatically +1. When the. View is recycled, the. The reference count of V will become 0;  V will be recovered by the system.
(iv) If V is a property of a class and is reatin when declared
[OBJC]View Plaincopy
    1. @property (nonatomic, retain) UIView *v; or @property (nonatomic, copy) UIView *v; declared properties, So allocate memory like this
    2. v = [[UIView alloc] init];
    3. [self. View addsubview:v];
    4. [v release]; This is the case with a, which does not need to be released in the Dealloc.
[OBJC]View Plaincopy
  1. But if it is
  2. Self. v = [[UIView alloc] init];
  3. [self. View addsubview:self. v];
  4. [self. V release]; add a self , then in the Dealloc [v release], because if the attribute is declared with Reatin, use the Self. When initializing, the object's reference technology automatically +1. self. v = [[UIView alloc] init]; This time the reference count is 2

      Chat autorelease,autorelease is a special mechanism of Objectivec language, and also the key to understanding Objectivec memory management. Autorelease actually just delayed the call to release, and for each autorelease, the system simply placed the object in the current Autorelease pool, and when the pool was released, All object in the pool is called release. Right, in fact autorelease is a mechanism of delayed release, a better introduction autorelease article: http://developer.51cto.com/art/201007/212523.htm   This article personally thinks that the autorelease explanation is more detailed, also easy to understand. I recommend you to look at, here in addition to this article I would like to talk about my understanding of autorelease. I understand that when the system executes each function, it will automatically open a autoreleasepool (remember that each function will open a autoreleasepool, not every class to open one, which is critical), All objects declared with Autorelease in this function will be placed in the pool of this one. When the function is finished, this one autorelasepool will be drain off, then all the reference count in the pool is 0 (remember is the reference count is 0) object, it will be collected by the system. Written here a lot of people will program apes will question me, such as I wrote in a Uiviewcontroller class Viewdidload function: UIView *myview = [[UIView alloc] init] Autorelease ]; [Self.view Addsubview:myview]; When the Viewdidload function finishes executing, why the MyView object is not recycled. This is because [Self.view Addsubview:myview] was called, so MyView's reference count became 2, and when the function was executed, autorelease was drain out, Automatically frees all objects in the pool to call the release method, at which point the reference count of all objects-1, if the object's reference count is 0 then it will be reclaimed by the system, if not 0, then he is also retained, that is, the automatic release of the pool objects are not destroyed by the automatic release of the pool, Automatic release of the poolObjects are only deferred, and the release of objects in the pool satisfies the mechanism of reference counting.     Above is my simple understanding of iOS memory management, the wrong place to welcome you to correct, we improve each other.

[Reprint] A little summary and understanding of memory management in iOS development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.