Objective-C Memory Management Mechanism

Source: Internet
Author: User

Objective-C memoryThe management mechanism is the content to be introduced in this article. Recently, the iphone platform is quite popular, and everyone is on it. However, it seems that many programmers are transferred from Java and are used to Java.Objective-CIt may be difficult to adapt, especiallyObjective-COfMemoryManagement mechanism.

The Mobile Phone platform is different from the computer. Although the applications developed are small applications, but due to the limitations of the mobile phone hardware, if the memory is not properly managed, there will still be many problems, especially Java programmers who are used to the garbage collection mechanism are most likely to ignore this issue. I hope the following article will help you.

Objective-C uses a mechanism called Hold Count) to manage objects in memory.

In Objective-C, each object corresponds to their own Hold Count Retain Count). The Hold Count can be understood as an integer counter. when an object is created using the alloc method, the hold count is automatically set to 1. When you send a retain message to an object, the hold count increases. On the contrary, when you send a release message like an object, the holding Count value decreases. When the holding count of an object changes to 0, the object will release the memory occupied by it.

Why is the concept of holding count used?

Imagine that sometimes you reference the same object in multiple different objects. When you release this public object in one place, it will inevitably affect other objects that reference this public object. However, whenever you reference this public object, a retain message is sent to the hold count, and a release message is sent when an object is released. In this way, the object holding count records how many objects reference itself. When its holding count changes to 0, this means that no other object references this object, so it can safely release its memory.

Let's assume that we have a dog and several people holding the dog with a rope, so the last person could not loosen the rope in his hand, this dog cannot be free. The purpose of holding the count is to record how many people hold the rope.

Unlike Java garbage collection, Objective-C provides a special mechanism for holding the count. It gives developers more control over when and how to release an object, but it also requires developers to be more careful. If you release an object too early, your application may crash unexpectedly. On the contrary, if you do not release the memory occupied by objects for a long time, the application may cause memory leakage after running for a period of time.

Array) is a special example when you add an object to the Array. What is stored in the array is not a copy of the object, but a pointer to the object. When the array saves the pointer, it will send a retain message to the object indicated by the pointer. Correspondingly, the holding count of the object will increase. When the object is removed from the array, the release message is also sent to the object, and the holding count of the object is reduced. When the array is released, the release message is sent to all objects in the array. Let's look at the two examples below:

1. No memory version is released

 
 
  1. array = [[NSMutableArray alloc] init];  
  2. for ( i = 0; i < 10; i++) {  
  3. newNumber = [[NSNumber alloc]initWithInt:(i * 3)];  
  4. [array addObject:newNumber];  

When creating a newNumber object, the code above sends a retain message to the object, and the hold count of the object changes to 1. When the reference of this object is added to the array, a retain message is sent to the object, so that the object's Hold count is changed to 2. When array is used up, we will habitually release the array, but this will not release the object held by the array, but only change the holding count of all objects to 1, these objects will still occupy the memory.

2. Release the memory version

 
 
  1. for (i = 0; i < 10; i++) {  
  2. newNumber = [[NSNumber alloc]initWithInt:(i*3)];  
  3. [array addObject:newNumber];  
  4. [newNumber release];  

Summary: DetailsObjective-C memoryThe management mechanism has been introduced. I hope this article will help you!

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.