About memory management in OC, alloc, retain, release, copy, dealloc

Source: Internet
Author: User

We all know that the memory of a mobile phone is limited, and every mobile phone app requires a certain amount of space. When the app occupies too much space, the system will issue a warning, how can we achieve more efficient, practical, and beautiful results in a limited space?

At this time, memory management is involved in the OC.

In the language oC, there is no garbage collection mechanism, but it uses another form or method to achieve the effect of space collection, that is, reference the counter.

Don't look at the reference counter. This name is very big. It is actually an integer.

Therefore, four bytes are allocated to the OC to store the data.

There are only two types of reference count values: 0 and non-0. We know that computers are actually very stupid. when the results are only these two types, they can be judged and there are more results, it will also be confused.

When the reference count is 0, the system will destroy the space,

When reference count! = 0, the system will reserve space.

However ,! = 0, contains 1.2.3..., a series of integers. At this time, we need to use retain.

Before explaining retain, let me first talk about alloc. The system can destroy space, but there is no space. How can the system destroy it? The role of alloc is to create a space, which makes the reference count of the object from "no"-1. Note: It is not 1, not 0 to 1.

Next, we will use a real-world example to illustrate more interesting points:

I assume that the created object is a dog with four persons: A, B, C, D, and

Alloc ---- (A bought a dog and started walking the dog with a rope) --- count as 1

Retain --- (B also wants to walk the dog, so he also finds a rope tied to the dog) --- count + 1 to 2

Retain -- (Bing wants to like this dog, so he also finds a rope and walks the dog together) ---- count + 1 to 3.

At this time, Ding saw and wanted to walk the dog, but it was so pitiful to see that three people had slipped away from the dog. So he bought an identical dog from the flower bird market, copy is your own slide. count + 1. After that, the Count of this object is only 1. The original object is 3, which has not changed.

But all three of them went home for dinner. Every time they left alone, there would be one less rope, 3-1-1-1. When it was 0, our pervasive urban management system will detect a stray dog in a certain place. Someone will come and take the dog away immediately. This is dealloc.


About memory management in OC, alloc, retain, release, copy, dealloc

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.