Analysis of memory management mechanism of reference count in iOS

Source: Internet
Author: User

Reference counting in IOS is the way memory is managed, although in the iOS5 version, the automatic reference count management mode has been supported, but understanding how it works helps us understand how the program operates and helps debug programs.

the memory management of the operating system is divided into heaps and stacks.

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

NSString defines an object that is saved in the stack, so it does not reference the calculation. Read some books saying its reference calculation would be the fffffffff maximum integer, the test results show that it is- 1. retain the object , it is not good to change its retaincount value.

an object defined by mutablensstring needs to allocate the memory space in the heap before initialization can be used. It uses reference counting to manage memory. The retaincount operation of the object is incremented one at a time.

In fact, the reference count is the spatial management of the memory area, which should be viewed from the memory block perspective. Any object is a pointer to it, and the number of pointers to it, the number of reference calculations.

If there is no pointer to the memory block, it is clear that the memory block there is no object reference, the reference calculation is 0, the system will be free for the memory area, and then immediately clean up, that is, update the management heap in the list of a certain marker.

(Miki West Tour @mikixiyou original link: http://mikixiyou.iteye.com/blog/1592958 )

The test method is as follows:

Create a non- arc project in Xcode with a single view. Create a button action method.

-(Ibaction) TESTRC: (ID) Sender {

Nsinteger i;

I=self.i_test;

if ((i%2) ==1)

{

NSString * [email protected] "welcome";

NSString * [email protected] "MLGB";

NSString * STR3;

NSString * [email protected] "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 use causes crash because the 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 results are as follows:

2012-07-14 11:07:38.358 testmem[878:f803] str1 addr is 0x3540

2012-07-14 11:07:38.360 testmem[878:f803] str2 addr is 0x0

2012-07-14 11:07:38.361 testmem[878:f803] STR3 addr is 0x0

2012-07-14 11:07:38.362 testmem[878:f803] STR4 addr is 0x3540

In the stack, objects of the same content, str1 and STR4 , are allocated in a memory area, which is the function of the C compiler, which facilitates memory usage and efficiency.

2012-07-14 11:07:38.363 testmem[878:f803] str1 retaincount is-1

2012-07-14 11:07:38.364 testmem[878:f803] str2 retaincount is-1

2012-07-14 11:07:38.365 testmem[878:f803] str3=[str1 retain];

2012-07-14 11:07:38.366 testmem[878:f803] str1 retaincount is-1

2012-07-14 11:07:38.367 testmem[878:f803] STR3 retaincount is-1

2012-07-14 11:07:38.367 testmem[878:f803] str3=[str2 retain];

2012-07-14 11:07:38.368 testmem[878:f803] str2 retaincount is-1

2012-07-14 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);

 /*

2012-07-14 11:07:36.652 testmem[878:f803] mstr1 addr is 0x68706b0

2012-07-14 11:07:36.655 testmem[878:f803] mstr2 addr is 0x6876040

2012-07-14 11:07:36.656 testmem[878:f803] MSTR3 addr is 0x2a35

2012-07-14 11:07:36.657 testmem[878:f803] MSTR4 addr is 0x686fbf0

2012-07-14 11:07:36.657 testmem[878:f803] mstr1 Retaincount is 1

2012-07-14 11:07:36.658 testmem[878:f803] mstr2 Retaincount is 1

2012-07-14 11:07:36.659 testmem[878:f803] mstr3=[mstr1 retain];

2012-07-14 11:07:36.660 testmem[878:f803] mstr1 Retaincount is 2

2012-07-14 11:07:36.660 testmem[878:f803] MSTR3 Retaincount is 2

2012-07-14 11:07:36.661 testmem[878:f803] MSTR3 addr is 0x68706b0

2012-07-14 11:07:36.662 testmem[878:f803] mstr3=[mstr2 retain];

2012-07-14 11:07:36.663 testmem[878:f803] mstr2 Retaincount is 2

2012-07-14 11:07:36.663 testmem[878:f803] MSTR3 Retaincount is 2

2012-07-14 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 to its memory area, which is easy to understand from the memory block's point of view.

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.