Analysis of reference counting memory management mechanism in iOS and ios Memory Management

Source: Internet
Author: User

Analysis of reference counting memory management mechanism in iOS and ios Memory Management

In iOS, reference counting is the memory management method. Although the iOS5 version already supports the automatic reference counting management mode, understanding how it runs helps us understand how the program runs, helps debug programs.

The memory management of the operating system is divided into heap and stack.

 

The memory allocated in the heap is used to reference the counting mode; not in the stack.

 

The NSString object is stored in the stack, so it does not reference computing. I have read some books saying that its reference calculation will be the maximum integer of fffffff. The test result shows that it is-1. retainCount is not easy to change the retainCount value of this object.

 

The MutableNSString object must be allocated memory space in the heap before initialization. It manages memory by reference count. Perform the retainCount operation on this object to add one at a time.

 

In fact, reference counting manages the space in the memory area from the perspective of memory blocks. Any object is a pointer to it. If there are multiple pointers pointing to it, there will be several reference calculations.

If no pointer points to the memory block, it is obvious that the memory block has no object reference, and the reference calculation is 0. The system will clean up immediately because the memory area is idle, that is, update a flag in the linked list of the Management heap.

 

 

(Miki westward journey @ mikixiyou original link: http://mikixiyou.iteye.com/blog/1592958)

 

The test method is as follows:

 

Create a non-arc project in xcode, and create a single view. Create a button.

 

-(IBAction) testRC :( id) sender {

 

NSInteger I;

I = self. I _test;

 

If (I % 2) = 1)

{

NSString * str1 = @ "welcome ";

NSString * str2 = @ "mlgb ";

NSString * str3;

NSString * str4 = @ "welcome ";

NSLog (@ "str1 addr is % p", str1 );

NSLog (@ "str2 addr is % p", str3 );

NSLog (@ "str3 addr is % p", str3 );

NSLog (@ "str4 addr is % p", str4 );

 

NSLog (@ "str1 retainCount is % I", [str1 retainCount]);

NSLog (@ "str2 retainCount is % I", [str2 retainCount]);

// NSLog (@ "str3 retainCount is % I", [str3 retainCount]); this will cause crash because str3 does not point to any memory area.

 

 

Str3 = [str1 retain];

NSLog (@ "str3 = [str1 retain];");

NSLog (@ "str1 retainCount is % I", [str1 retainCount]);

NSLog (@ "str3 retainCount is % I", [str3 retainCount]);

Str3 = [str2 retain];

NSLog (@ "str3 = [str2 retain];");

NSLog (@ "str2 retainCount is % I", [str1 retainCount]);

NSLog (@ "str3 retainCount is % I", [str2 retainCount]);

 

/*

The result is as follows:

11:07:38. 358 testMem [878: f803] str1 addr is 0x3540

11:07:38. 360 testMem [878: f803] str2 addr is 0x0

11:07:38. 361 testMem [878: f803] str3 addr is 0x0

11:07:38. 362 testMem [878: f803] str4 addr is 0x3540

 

In the stack, all objects str1 and str4 with the same content are allocated in a memory area. This is the function of the c compiler and is conducive to memory usage and efficiency.

 

 

11:07:38. 363 testMem [878: f803] str1 retainCount is-1

11:07:38. 364 testMem [878: f803] str2 retainCount is-1

11:07:38. 365 testMem [878: f803] str3 = [str1 retain];

11:07:38. 366 testMem [878: f803] str1 retainCount is-1

11:07:38. 367 testMem [878: f803] str3 retainCount is-1

11:07:38. 367 testMem [878: f803] str3 = [str2 retain];

11:07:38. 368 testMem [878: f803] str2 retainCount is-1

11:07:38. 369 testMem [878: f803] str3 retainCount is-1

 

*/

}

Else

{

 

 

NSMutableString * mstr1 = [[NSMutableString alloc] initWithString: @ "welcome"];

NSMutableString * mstr2 = [[NSMutableString alloc] initWithString: @ "mlgb"];

NSMutableString * mstr3;

NSMutableString * mstr4 = [[NSMutableString alloc] initWithString: @ "welcome"];

 

NSLog (@ "mstr1 addr is % p", mstr1 );

NSLog (@ "mstr2 addr is % p", mstr2 );

NSLog (@ "mstr3 addr is % p", mstr3 );

NSLog (@ "mstr4 addr is % p", mstr4 );

 

NSLog (@ "mstr1 retainCount is % I", [mstr1 retainCount]);

NSLog (@ "mstr2 retainCount is % I", [mstr2 retainCount]);

// NSLog (@ "mstr3 retainCount is % I", [mstr3 retainCount]);

 

Mstr3 = [mstr1 retain];

NSLog (@ "mstr3 = [mstr1 retain];");

 

NSLog (@ "mstr1 retainCount is % I", [mstr1 retainCount]);

NSLog (@ "mstr3 retainCount is % I", [mstr3 retainCount]);

NSLog (@ "mstr3 addr is % p", mstr3 );

 

Mstr3 = [mstr2 retain];

NSLog (@ "mstr3 = [mstr2 retain];");

NSLog (@ "mstr2 retainCount is % I", [mstr1 retainCount]);

NSLog (@ "mstr3 retainCount is % I", [mstr2 retainCount]);

NSLog (@ "mstr3 addr is % p", mstr3 );

 

/*

 

11:07:36. 652 testMem [878: f803] mstr1 addr is 0x68706b0

11:07:36. 655 testMem [878: f803] mstr2 addr is 0x6876040

11:07:36. 656 testMem [878: f803] mstr3 addr is 0x2a35

11:07:36. 657 testMem [878: f803] mstr4 addr is 0x686fbf0

 

11:07:36. 657 testMem [878: f803] mstr1 retainCount is 1

11:07:36. 658 testMem [878: f803] mstr2 retainCount is 1

 

11:07:36. 659 testMem [878: f803] mstr3 = [mstr1 retain];

 

11:07:36. 660 testMem [878: f803] mstr1 retainCount is 2

11:07:36. 660 testMem [878: f803] mstr3 retainCount is 2

 

11:07:36. 661 testMem [878: f803] mstr3 addr is 0x68706b0

 

11:07:36. 662 testMem [878: f803] mstr3 = [mstr2 retain];

 

11:07:36. 663 testMem [878: f803] mstr2 retainCount is 2

11:07:36. 663 testMem [878: f803] mstr3 retainCount is 2

11:07:36. 664 testMem [878: f803] mstr3 addr is 0x6876040

 

 

*/

 

 

}

 

Self. I _test = self. I _test + 1;

 

}

 

In short, the reference count is actually the number of pointers pointing to the memory area. It is easy to understand from the perspective of memory blocks.


Summary of Objective-c memory overflow experience, sharing with others

The memory of iOS platform uses the reference counting mechanism and introduces the semi-automatic release mechanism, as a result, developers are very prone to memory leakage and inexplicable memory growth in terms of memory usage. This article will introduce the memory usage principles and usage traps of the iOS platform, and deeply analyze the autorelease mechanism; the process of handling low-memory alarms, and introduces the trace records of memory surges and the usage of related tools based on your instance;
IOS platform memory FAQ
As a developer of iOS platform, have you ever been troubled by memory problems? Memory continues to grow inexplicably, crash programs, and hard-to-find memory leaks are common issues related to the memory of the iOS platform. This article will introduce the memory management mechanism of the iOS platform in detail, the autorelease mechanism and memory usage traps will solve most of the memory problems on the iOS platform and improve program stability;
1 Introduction to memory management on iOS platform
The memory management of iOS platform uses the reference counting mechanism. when an object is created using the alloc or allWithZone method, the reference counting will be + 1. When the released object uses the release method, the reference count is-1, which means that each object will track the number of other objects that reference it. Once the reference count is 0, the memory of the object will be released. In addition, iOS also provides a latency release mechanism, AutoRelease, to apply for memory in this way, developers do not need to manually release, the system will release the memory at a certain time; due to the diversity of memory management on the iOS platform, developers are prone to memory leaks or program crashes during memory usage, this article will introduce in detail the usage specifications and technical skills of iOS platform memory and how to use tools to avoid or discover problems;
2 iOS platform memory usage principles
2.1 object ownership and destruction
2.1.1 Who created and released;
If the object is created using alloc, new, copy, and mutableCopy, you must call the release or autorelease method to release the memory;
If it is not released, the memory will leak!
2.1.2 who retain and who releases;
If a retain message is sent to an object, the reference count will be greater than 1, the release or autorelease method must be sent after use to release the memory or restore the reference count;
If it is not released, the memory will leak!
2.1.3 not created and retain not released;
Do not release objects that are not alloc or retain, otherwise the program will crash;
Do not release the autorelease object, otherwise the program will crash;
2.2 object deep copy and shortest copy
Generally, copying an object includes creating a new instance and initializing the instance with the value in the original object. Copying a non-pointer instance variable is simple, such as Boolean, integer, and floating point. There are two methods to copy the instance variables. A method is called shortest copy, which copies the pointer value of the original object to the copy. Therefore, the original object and the copy share the reference data. Another method is deep copy, that is, copy the data referenced by the pointer and assign it to the instance variable of the copy.
2.2.1 deep copy
In the deep copy process, a new object is created and the reference count is 1. The new object is initialized with the value of the old object;
ClassA * objA = [[ClassA alloc] init];
ClassA * objB = [objA copy];
ObjB is a new object with a reference count of 1, and the objB data is equivalent to the objA data;
Note: The objB needs to be released, otherwise it will cause memory leakage!
2.2.2 shallow copy
The process of copying objects is to add the reference count of the original object + 1 without introducing new objects.
ClassA * objA = [[ClassA alloc] init];
ClassA * objB = [objA retain];
Note: The objB must be released to restore the reference count of objA. Otherwise, memory leakage will occur!
2.3 object access method 2.3.1 attribute declaration ...... remaining full text>

Ios Memory Management Mechanism

When the background does not occupy cpu but memory usage, some programs will be automatically closed when the program is opened to free up storage.

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.