Overview of the reference count memory management mechanism in iOS (NSString reference count is-1)

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 applied to the reference counting mode, but not in the stack.

the object defined by NSString is stored in the stack, so it does not have a reference count and is not managed by reference counting on memory. Before, I was on a blog postLook, the reference count of the constant will be a very large integer, and the result of the test shows that it is-1. The retain operation of the object does not change its retaincount value.

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

In factreference counting is the spatial management of memory areas and should be viewed from the perspective of memory blocks. Any object is a pointer to a memory block, how many pointers point to the memory block, and how many references the memory block has.
If there is no pointer to the memory block, it is clear that the memory block has no object reference, the reference calculation is 0, the system will think that the memory area is idle, and then immediately clean up, that is, update the management heap in the list of a certain marker bit.

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); You will find str1 and str2 two pointers pointing to the same memory
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]); STR1 and STR2 are created in the heap, so you cannot use reference counting to manage memory, here is-1
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:
2014-10-09 17:12:57.887 mutablecopy) [4402:60b] str1 addr is0x3598
2014-10-09 17:12:57.888 mutablecopy) [4402:60b] str2 addr is 0x35a8
2014-10-09 17:12:57.889 mutablecopy) [4402:60b] STR3 addr is 0x1
2014-10-09 17:12:57.889 mutablecopy) [4402:60b] STR4 addr is0x3598

In the stack, the content of the same objects str1 and STR4, are allocated in a memory area, this is the function of the C compiler, in favor of the efficiency of memory use.

2014-10-09 17:20:57.132 mutablecopy) [4504:60b] str1 retaincount is-1
2014-10-09 17:20:57.132 mutablecopy) [4504:60b] str2 retaincount is-1
2014-10-09 17:20:57.132 mutablecopy) [4504:60b] str3=[str1 retain];
2014-10-09 17:20:57.133 mutablecopy) [4504:60b] str1 retaincount is-1
2014-10-09 17:20:57.134 mutablecopy) [4504:60b] STR3 retaincount is-1
2014-10-09 17:20:57.134 mutablecopy) [4504:60b] str3=[str2 retain];
2014-10-09 17:20:57.135 mutablecopy) [4504:60b] str2 retaincount is-1
2014-10-09 17:20:57.135 mutablecopy) [4504:60b] 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]); This use causes crash because STR3 does not point to any memory area.

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

   In the heap, objects of the same content, str1 and STR4, are allocated in different memory regions.

2014-10-09 17:32:49.783 mutablecopy) [4621:60b] Mstr1 Retaincount is 1
2014-10-09 17:32:49.783 mutablecopy) [4621:60b] Mstr2 Retaincount is 1
2014-10-09 17:32:49.784 mutablecopy) [4621:60b] mstr3=[mstr1 retain];
2014-10-09 17:32:49.784 mutablecopy) [4621:60b] Mstr1 Retaincount is 2
2014-10-09 17:32:49.785 mutablecopy) [4621:60b] Mstr3 Retaincount is 2
2014-10-09 17:32:49.785 mutablecopy) [4621:60b] MSTR3 addr is 0x8e0e000
2014-10-09 17:32:49.785 mutablecopy) [4621:60b] mstr3=[mstr2 retain];
2014-10-09 17:32:49.785 mutablecopy) [4621:60b] Mstr2 Retaincount is 2
2014-10-09 17:32:49.785 mutablecopy) [4621:60b] Mstr3 Retaincount is 2
2014-10-09 17:32:49.786 mutablecopy) [4621:60b] MSTR3 addr is 0x8e0daa0


*/


}

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.

Overview of the reference count memory management mechanism in iOS (NSString reference count is-1)

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.