IOS memory management programming guide

Source: Internet
Author: User

Http://blog.csdn.net/lifengzhong/article/details/7739496

The basic data type is value type, which is stored on the stack and allocated and recycled by the compiler. The programmer can use it immediately after it is declared. NSObject objects and their subclasses are of reference type and stored on the stack. The programmer is responsible for allocation and collection. Objects should be released immediately after they are used up. In some cases, objects are assigned and initialized and passed to another method. In this case, the programmer no longer has control over the object, you can call the autorelease method of an object to throw the object to the automatic recycle pool, which is less efficient. If you have ownership of an object, you need to take charge of its recycling work. Otherwise, you do not need it or retrieve the objects you do not own.

1: All objects returned by functions starting with alloc, new, copy, or mutabelCopy and these keywords are owned by you, that is, the memory Recycle of these objects. This is a convention in iOS development. Therefore, when writing your own alloc, new, or copy functions, follow this naming convention.

2: The object returned by retain has ownership. For example, display the result returned by calling the retain function or a member variable of the retain type of synthesize. 3: no ownership of all objects returned using other functions.

4: the reference of the returned object, with no ownership.

5: The object returned by autorelease has no ownership.

After the methods are used to declare and assign objects, the programmer does not need to do anything to release the objects. After the CPU command leaves the method, the allocated objects defined in the method are automatically released. Instance variables/attributes are different. In a method, if we are sure that we no longer need an instance variable/attribute, we can set the nil value for the object, to ensure that the resources occupied by class instance variables/attributes are correctly released after the CPU command leaves this type of instance. Generally, the instance variable/attribute is set to nil in the viewDidUnload function.

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.