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.

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.