Objective-C memory management I understand

Source: Internet
Author: User

Objective-C memory management I understand

When I started to learn iOS, I already had the ARC feature. So I started to learn it in the ARC environment. Although I have some knowledge about memory management, however, I did not seriously deal with this issue. The projects at work started development before and used non-ARC. The project has been initially developed and cannot be changed to ARC. So I went back to study memory management. At the beginning, I was dizzy. I didn't know where to change retain or where to release the event. But later I knew a lot about it, and I felt like these things. I felt like I knew the program was running right, I think it is very good. In the future, it is estimated that there will be very few new projects without using ARC. I just think that memory management is quite interesting and I have carefully studied it. I will summarize it.

1. structure:

From the object point of view, the entire program is A nested structure. First, it is A. Many other objects will be used during the construction and running of, other Objects will use some other objects, which are very similar to the json structure. Why do we need to see the program first? In this way, we can regard the entire program as an object containing the structure of many other objects, so that memory management can be divided into two parts: (1) release the objects to be released (2) when an object is released, all the objects it uses are released (except for some objects that are used by other objects)

For example, if the ViewController is pushed to a ViewController and then returned from the viewController, The Viewcontroller will be released, which generally does not cause errors. But is the member variables of this ViewController released? Have all the temporary objects created in this ViewController method been released? Then you may have used a custom object in this viewContrller, which is assumed to be a pruductCell. When you have done a good job of changing and releasing this object, you can check it again. m file, whether the object it uses is released correctly.

I think it is good to check such a structure.

2. When an object is released, all the objects it uses are released (except for some objects that are used by other objects ):

First, the reference and release of objects should follow the following principle: release for your retain, not for your retain (assumed as criterion 1)

Other Objects used by an object (assuming A) can be divided into two types: A temporary object (assuming B), which can be released after use; another is the object property and member variable (assuming C), which is usually used repeatedly in each method of object, in this way, object A can be released when it is released, that is, it is released in the dealloc method of object.

Temporary object:

This is easy to handle and will be released after use:

UIView * subView = [[UIView alloc] initWithFrame: CGRectMake (30,100,150,260)]; [self. view addSubview: subView]; // addsubView retain subView [subView release];
The release here is not a value to release the memory, it is to release the ownership of this temporary object, it is to execute the release. For ownership, I understand that object A has retain (including alloc, copy, etc.) on an object (assumed as B ), in this way, if other objects that reference this object (assuming C), as long as the criterion 1 is followed, after Object C release object B, object B will return to the previous reference count. In this way, as long as object A does not release object B, the memory of object B will never be released. This is like object A has object B.

For the temporary object release, when the object that references it also release it, it will not be owned by any object, and the memory will be released, which is just like this. For example, when the subView is removed from the self. view, the subView is basically not needed. In this case, the memory is released.

Because the temporary variables are built in the method, the pointer is lost when this method is used, and the ownership of the method cannot be released. Therefore, the release must be performed before the method ends.

Attributes and member variables:

In order to keep the attributes and member variables of an object (A) alive throughout the process, release is not allowed; otherwise, the reference count may be 0, as a result, the memory has been released at the next time, and the program crashes:

_ SubView = [[UIView alloc] initWithFrame: CGRectMake (30,100,150,260)]; [self. view addSubview: _ subView]; [_ subView release]; [_ subView removeFromSuperview]; NSLog (@ "% @", _ subView); // program crash
It does not mean that the member variables will not be released, but will be released, and will not be used again next time. Since it is a member variable and attribute, it will be used in multiple methods and even be used by other class objects, so it should be "active ".


3. Keywords retain and assign for Attribute definition:

When you use attributes to define variables, the set \ get method is provided. If you use self when assigning values to variables. XXX = YYY is used to assign values. In fact, the set method is called, and the problem related to memory management is: If retain is used for Attribute definition, the set method is:

-(void )setXXX:(UIView *)XXX{    [XXX retain];    [_XXX release];    _XXX = XXX;}
The new value will be retained, and the old value will be release, so that the incoming YYY will get the ownership, and ensure that its memory will never be released when self uses it. This also follows rule 1. The use of assign is a simple assignment without retain \ release operations.

If the attribute is assign or self. XXX is not used for value assignment, the retain object will not be assigned:

UIView * view1 = [[UIView alloc]initWithFrame:CGRectMake(30, 100, 150, 260)];    _XXX = view1;    [view1 release];
It won't crash here, But _ XXX becomes a zombie pointer, and the memory pointing to the location is released, which is also a vulnerable part of assgin, therefore, it takes a long time to use an object. It is best to use retain to ensure that the reference count is not 0.

4. About autoRelease:

First, construct the object using the class method. The returned value is usually autoRelease.

I understand why autoRelease is used to delay release. Because of my build, I want to release it. But if I release this object, the memory will be released. Isn't it a white build? Can be built, and then it is given to the desired place, and then released. For example:

-(NSString *)createString{    NSString * str = [[NSString alloc]init];    return str;}
If I do not release the str file in the method and follow Rule 1 when using it outside, the reference count of this str file will remain at 1 and will not be released. This is a problem. But if it is released, the return value is a null value, which makes no sense. When autoRelease is used, you can return a meaningful value, and the object can be released in the automatic release pool, so you don't have to worry about memory leakage.

5. Special Methods and objects:

AddSubview will retain the added view, and adObject of NSMutableArray will also retain the added object. The block will retain all the variables used in it, which may cause loop reference, when nsttain is not released, it will retain build as the specified target. To release the target, you need to stop NStimer first.

We do not need to manage the memory for string constants.


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.