Memory management of iOS learning notes

Source: Internet
Author: User

One, memory management type definition

1, the basic type of any C type, eg:

Int,short,char,long,long long,struct,enum,union are basic types or structures

Memory management is not valid for the basic type of C language

2,OC type (non-basic type)

Any object that inherits from the NSObject class belongs to the OC type

Which is the other type except for C.

Second, OC object structure

All OC objects have a counter that retains the number of references to the current object, and if the counter is 0, the object is actually freed.

Three, alloc retain release function

1, the Alloc function is created using the object, and the counter is 1 after creation (only once)

Retain is counter +1 to an object (can be called multiple times)

Release is a counter to an object-1 (minus 0 objects will be freed from memory)

A reference counter is implemented in the 2,oc class, and the object knows the number of times it is currently referenced.

If you need to reference an object, you can send an retain message to the object so that the object counter is + 1;

When you do not need to reference the object, you can send the release message to the object, so that the object counter is-1;

When the counter is reduced to 0, the object's Dealloc function is automatically called, and the object frees the memory;

Objects with a counter of 0 can no longer use release and other methods.

IV, automatic release of the pool

The auto-free pool is an automatic memory recovery mechanism for OC, which can be used to collect temporary variables through the automatic release pool.

When the auto-release pool itself is destroyed, all objects inside the pool do a release operation once.

Any OC object that calls the Autorelease method will place the object in its closest auto-release pool (the release pool at the top of the stack).

Five, not an object created with Alloc,retain,copy, cannot be released with release.

Memory management of iOS learning notes

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.