OC Learning notes the memory management of the object

Source: Internet
Author: User


One, why to do memory management
Compared with the hard disk which is now on T, although the computer's memory is also in the upgrade or too small, and now the application software is also very eat memory, program running process if not to manage memory, if there is leakage, system memory will be used more and more small, for mobile devices is more so. The memory of the Apple handset is only so big, if the application does not manage memory to occupy excessive memory, the system certainly is not allowed, the flash that throws to give the user bad experience, that this is certainly not a successful application development.
Programs in operation, should be destroyed in time no longer needed variables or objects, the timely release of memory to maintain the dynamic balance of memory footprint.
Second, the objective of the OC memory management
The goal of memory management in OC is primarily any OC object created after inheriting the root class nsobject, we don't need to care about the basic data type, and the system can be recycled automatically. Because the basic data type in memory differs from that of the OC object, the former is stored in stacks and the latter is stored in heaps.
For example: int a = 1; int b = 2; Person *p = [[Person alloc] init]; These are the two categories where the code is stored in memory. Little bridge here recalls a picture of MJ's teacher:

As shown in the figure, the basic data type variables are stored temporarily by the stack, and are automatically recycled when the system expires, such as the following test picture:

The figure above shows that the memory of a variable of the base data type is automatically freed before the main function return, and that the OC object always occupies memory, noting:
The person type pointer p Here is also the basic type and will be reclaimed by the system when it expires, where the scope is the main function, so the P pointer variable is still there before return. But the person object that P points to is occupied by memory until the end of the program. If P and a, b are the local variables in Mian, then p may be released before return, and if P is released then the problem is that the object it points to will become an anonymous object because the object is still there, You will not be able to release it later, unless the program exits.

The reference counters         OC objects in the OC object have 4 bytes of space to store the reference counter

Role:
    The      Reference counter represents the number of times the object is referenced, alloc, new, or copy out of a novel object, and its reference counter initial value is 1.
Once its reference counter is 0, the system reclaims the object mercilessly, and conversely the reference counter is not 0 so it always occupies memory. So as you think, OC memory management is done by balancing the reference counter, as long as the reference counter is guaranteed to become 0 when the object expires, then the program will be able to dynamically release expired objects, and avoid memory leaks. However, in the case of small bridges, keeping the balance of object reference counters is far less straightforward than it seems.
        For a single individual object, the counter plus 1 minus 1 is clearly understood, but when the object is linked to the object, when the counter to each other and 1 when minus 1 becomes more complicated.     Operations:
          Each object has three inherited from the root class method associated with memory management.
Retain: Sends the RRATIN message to the object, adds the object's reference counter by 1, and returns the object itself;
Release: Sends a release message to the object, reducing the object's reference counter by 1, no return value;
Retaincount: Gets the value of the object's current reference counter;  
        The first two are ways to maintain the balance of the OC Object reference counter. Managing memory is by properly calling these two methods, so that objects can be reclaimed by the system when they expire, rather than wait until the program exits to be recycled, and if the program is running from the phone, the expired object does not occupy memory.  

Four, memory management related to several concepts
Zombie Objects: Objects that have been recycled by the system are called Zombie objects and cannot be accessed again. Take a look at the bridge cancel the arc and open the Xcode zombie Object-managed test:

Wild pointers: Pointers to zombie objects or inaccessible memory are called wild pointers, for example, the p in the above image is a wild pointer, and using a wild pointer to invoke the method will show the error in the diagram above.
Null pointer: p = nil, or P =null, or p = 0; these three are null pointers, in contrast to wild pointers, it is not an error to call a method with a null pointer in OC.
V. Words of the object
Finally, the bridge review the message that the system sends to the object before it is recycled, which is also the last action of the object------dealloc
The OC object calls this method before it is destroyed to release the resources it occupies, then calls the parent class's Dealloc method, makes the parent class do the same, and finally completes the release of all resources. This method is overridden when managing memory, and the bridge rewrites this method:

//  files: person.m
//  Items: Blog notes////  Author: Buried Huaqiao
//  Date: 14-5-8
//  Copyright:  Copyright (c) 2014 itcast. All rights reserved.
The

Dealloc method was called #import "Person.h" @implementation person

-(void) dealloc
{
    NSLog (@). ");
    [Superdealloc];
}

@end






Related Article

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.