ARC & MRC String Memory management strategy research
Two days ago, arguing with colleagues about whether the nsstring will change after the copy operation, two people to complete a half-day, the last write code verification, found that the original NSString operation is not as simple as we thought, Let's take a look at the NSString and nsmutablestring in the MRC under the Retain,copy,mutablecopy, as well as different modifications under the arc __weak, __strong decoration assignment what happened.
First, the verification code is as follows:
- (void) teststringaddress{intA =0; intb =0; Static intc =0; NSString*str =@"Hello World"; #if__has_feature (OBJC_ARC)__weak NSString*weakstr =str; __strong NSString*strongstr =str;#elseNSString*retainstr =[str retain];#endifNSString*copystr =[str copy]; Nsmutablestring*mutablecopystr =[str mutablecopy]; //Verify that the mutablecopy is mutablestring if it is not performing this line crash[Mutablecopystr AppendFormat:@".."]; STR=@"i ' m changed"; NSString*STR2 = [NSString stringWithFormat:@"Hello World"]; #if__has_feature (OBJC_ARC)__weak NSString*WEAKSTR2 =str2; __strong NSString*STRONGSTR2 =str2;#elseNSString*RETAINSTR2 =[str2 retain];#endifNSString*COPYSTR2 =[str2 copy]; NSString*COPY2STR2 =[str2 copy]; NSString*MUTABLECOPYSTR2 =[str2 mutablecopy]; NSString*MUTABLECOPY2STR2 =[str mutablecopy]; STR2= [[NSString alloc] Initwithformat:@"changed"]; Nsmutablestring*MUTABLESTR = [nsmutablestring stringwithstring:@"Hello World"]; #if__has_feature (OBJC_ARC)__weak nsmutablestring*weakmutablestr =Mutablestr; __strong nsmutablestring*strongmutablestr =Mutablestr;#elsensmutablestring*retainmutablestr =[mutablestr retain];#endifnsmutablestring*copymutablestr =[mutablestr copy]; Nsmutablestring*copy2mutablestr =[mutablestr copy]; NSString*mutablecopymutablestr =[Mutablestr mutablecopy]; NSString*mutablecopy2mutablestr =[Mutablestr mutablecopy]; [Mutablestr AppendFormat:@"apped Something"];#if__has_feature (OBJC_ARC)NSLog (@"\ r str:%@,\r weakstr:%@,\r strongstr:%@,\r copystr:%@,\r mutablecopystr:%@", str, WEAKSTR, STRONGSTR, Copystr, MUTABLECOPYSTR); NSLog (@"\ r str2:%@,\r weakStr2:%@,\r strongstr:%@,\r copyStr2:%@,\r mutableCopyStr2:%@", str2, WEAKSTR2, STRONGSTR2, COPYSTR2, MUTABLECOPYSTR2); NSLog (@"\ r mutablestr:%@,\r weakmutablestr:%@\r strongmutablestr:%@,\r copymutablestr:%@,\r mutablecopymutablestr:%@ ", Mutablestr, Weakmutablestr, Strongmutablestr, Copymutablestr, mutablecopymutablestr);#else NSLog (@"\ r str:%@,\r retainstr:%@,\r copystr:%@,\r mutablecopystr:%@", str, RETAINSTR, COPYSTR, MUTABLECOPYSTR); NSLog (@"\ r str2:%@,\r retainStr2:%@,\r copyStr2:%@,\r mutableCopyStr2:%@", str2, RETAINSTR2, COPYSTR2, MUTABLECOPYSTR2); NSLog (@"\ r mutablestr:%@,\r retainmutablestr:%@,\r copymutablestr:%@,\r mutablecopymutablestr:%@", Mutablestr, Retainmutablestr, Copymutablestr, mutablecopymutablestr);#endif}
teststringaddress
The code begins by defining a variable of two int, primarily to print out the stack address of the current function, and to verify that the in-memory stack is growing from a high address to a low address. Use precompiled Macros #if __has_feature (OBJC_ARC) to detect whether an arc environment is currently in place and write different test codes based on different environments.
The address of the variable is output by the "P" command at the end of the LLDB command line by adding a breakpoint at the teststringaddress, for example: To view the memory address and content pointed to by STR, enter "P str".
Debug Environment XCode6 Beta4,ios8 SDK.
II. implementation of the MRC
The execution results are as follows:
Explain the content:
1, the first part
"P &str" means the address of the variable itself that prints str, and "P str" means the address of the content that the STR points to. As shown, the result of the "P &str" Print is 0xbff0aff8, the address of the local variable B is 0XBFF0AFFC, and we can see that the memory addresses of the two are connected because they are local variables inside the function body. The result of the "P str" Print is 0x000a7048, which shows that the storage area and function local variables of the content that Str points to are not together.
2, the second part
c is a static variable that I define in the function, "P &c" prints the address of 0x000a778c, and observes that the variable C and the function variable are not together. The static variable will only have one in the program while it is running, and it is located in the variable area of the program.
3, the third part
STR is defined as "nsstring *str = @" Hello World ";", debugging finds that Str points to a 0x000a7048 , and the resulting address for the retain and copy operations performed on STR is 0x000a7028. The NSString address obtained after executing the mutablecopy is 0x7974a110, and the address obtained by STR and the retain,copy operation is not together.
Summary: A variable of the form @ "Hello World" is in the constant area of the program, while NSString creates a new object when the object for the constant area first executes retain and copy, and then performs the same action to play the previously created object. The mutablecopy operation creates an object somewhere else and uses STR to complete the initialization operation.
4. Fourth part
str2 defined as " nsstring *STR2 = [nsstring< span class= "s2" > stringwithformat:@ "Hello World" 0x79749000 , The address of the string to be executed by the retain operation is 0x7974a5c0 , the address obtained by the copy operation is also Span style= "font-family: Imitation;" >0x7974a5c0 , the same address is obtained after the test discovery and the copy operation is performed on the str2. This seems to be the same as our usual "retain add reference technology, copy create new objects" concept does not match. The address given to str2 execution mutablecopy is 0x 7974a380 , re-created an object that matches our expectations.
Summary: NSString objects created using the stringWithFormat method default on the heap, and a copy is created for the first retain or copy operation of this class of objects, and all subsequent retain and copy points to the same copy that was created earlier. A new object is created whenever the mutablecopy operation is performed.
5. Fifth part
the MUTABLESTR is defined as " [nsmutablestring stringwithstring:@ "Hello World"]; , the printing results found that the retain operation on Mutablestr gets the same address and mutablestr as the object, and the copy operation creates a new object, and the mutablecopy operation creates a new object.
Summary: nsmutablestring objects created using the Stringwithstring method default on the heap, and no new objects are created when retain is executed against nsmutablestring, and both copy and Mutablecopy create new objects.
Observe the above object address, can be divided into four intervals 0x000a70xx,0x000a78xx,0x7974xxxx,0xbff5xxxx, In fact, they represent four different memory segment constant area, static variable area, heap area, stack area respectively.
Iii. Performance under Arc
Execution results such as:
The analysis method is the same as the first part, which is not repeated here.
Summarize:
1, for the NSString object of the constant area, perform Weak,strong assignment the copy operation will only generate one copy, and a new object will be created each time the mutablecopy is executed.
2. When performing weak,strong,copy operations on the NSString object on the heap, only one copy is created, all subsequent operations are given the same object, and a new object is created each time the mutablecopy is executed.
3. The first Weak,strong operation for a Nsmutablestring object creates only one copy, all subsequent operations get the same object, and each copy and Mutablecopy operation creates a new object.
Iv. Summary
In the case of Arc and MRC under nsstring,nsmutablestring perform Retain,copy,mutablecopy,weak,strong operation, the memory is shown in the following table:
Note: Smileevday reserves all rights to this article
Reprint please famous source, have any questions welcome message
If you feel that this article has helped you, please recommend to the friends around