[OC learning-11] alloc, assign, new, retain, copy, mutablecopy, and release in arc and Memory Management

Source: Internet
Author: User

Generally, when developing a program, we only need alloc and do not need to manage release, because there is arc to help us manage it. However, you still need to know when learning: the memory is limited. If you do not need the memory allocated in the heap area, you need to recycle it. Otherwise, the memory will crash if it is not enough.


So the principle is: if there is allocation, there will be recycling. However, this allocation may be allocated several times. How can this problem be solved? This isReference count. When creating an object, it comes with this reference count.

(1) When alloc, new, copy, mutablecopy, and retain are used, the reference count is + 1, that is, retaincount + 1;

(2) When release is used, the reference count retaincount is reduced by 1.

(3) The missing assign will not add a reference count + 1, because assign is equivalent to an alias for the original object pointer, but the pointer address and the content pointed to by the pointer remain unchanged.

Specific respectively, You can see xiaoxuan%315 before an article: http://blog.csdn.net/xiaoxuan415315/article/details/8061084


We also know that:

// The following statement has the same effect as asstudent * Zhang = [[asstudent alloc] init]; asstudent * Zhang = [asstudent new];


[OC learning-11] alloc, assign, new, retain, copy, mutablecopy, and release in arc and Memory Management

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.