Objective-C Memory Processing Mechanism

Source: Internet
Author: User

I have studied objective-C over the past few days and briefly talked about my understanding and views on its memory management mechanism. If you have any ideas or different opinions, I can share them with you, first, objective-C uses the reference counter mechanism to manage the memory. When we declare an instance object and open up memory space for it in the heap, the reference count of its memory space is 1. If the release reference count is reduced by one, when the reference count is equal to 0, the system recycles the memory.

We all know that the reference in objective-C involves three methods:

1) Assign: fraction * B = [[fraction alloc] init]; fraction * A = B;

2) Retain: fraction * B = [[fraction alloc] init]; fraction * A = B; [B retain];

3) Copy fraction * B = [[fraction alloc] init]; fraction * A = [B copy];

The first assign is a simple value assignment, which enables a to point to the memory space in the heap directed by B. The reference count does not add 1, and any of them is 1. If we [A release], if the reference count is reduced by one, the reference count pointing to the memory is 0, and the memory is recycled, then B becomes a non-pointing wild pointer;

The second type of retain, we know that retain, reference count plus one, when we [B release], the reference count is 1, the memory will not be released, a can still point to, normal reference.

The third method is more flexible. We need to rewrite the copywithzone method to customize the copy. We can perform the shortest copy (return address, reference count without adding one) and retain, you can also directly open up a new memory space to copy the content for deep copy.

Therefore, we generally use Retain references. To prevent memory overflow and system crash caused by wild pointers, we need retain When referencing an instance object, release is required to end the reference. Why? This is to ensure that this reference can be normally performed without the influence of external release so that its instance variables become wild pointers, resulting in system crash. Note: Release and retain exist in pairs. We must remember release after Retain references.

In addition, we can also let the system automatically release the memory for us [[[fraction alloc] init] autorelease], but we 'd better manually manage the memory because it will be more efficient, system Management is equivalent to adding our memory address to a collection and scanning at intervals, which is far less efficient than manual management.

This is some of objective-C's personal understanding. I hope you can give some advice.

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.