Ios learning notes memory management, ios learning notes

Source: Internet
Author: User

Ios learning notes memory management, ios learning notes

1. Memory Management Type Definition
1. Any C type of the basic type. For example:
Int, short, char, long, long, struct, enum, union, etc. are basic types or struct
Memory Management is invalid for the basic type of C Language
2. OC type (non-basic type)
Any object inherited from the NSObject class belongs to the OC type.
That is, other types except C

Ii. OC object structure
All OC objects have a counter that retains the number of referenced objects. If the counter is 0, the object is actually released.

Iii. alloc retain release function
1. The alloc function is used to create an object. After creation, the counter is 1 (only once)
Retain is the counter + 1 for an object (can be called multiple times)
Release is an object counter-1 (when it is reduced to 0, it will be released from the memory)

2. The reference counter is implemented in the oc class, and the object knows the number of times currently referenced.
If you need to reference an object, you can send a retain message to the object, so that the object counter is + 1;
If you do not need to reference an object, you can send a release message to the object so that the object counter is-1;
When the counter is reduced to 0, the dealloc function of the object is automatically called, and the object will release the memory;
Objects whose counter is 0 cannot use release or other methods.

4. automatically release the pool
The automatic release pool is a memory automatic recovery mechanism of OC. Some temporary variables can be recycled and released through the automatic release pool.
When the automatically released pool is destroyed, all objects in the pool will perform a release operation.
Any OC object that calls the autorelease method puts the object in the Auto Release pool closest to itself (the release pool at the top of the stack ).

5. objects created using alloc, retain, and copy cannot be released using release.

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.