Exploration of string memory management policies in iOS: ARC & amp; MRC

Source: Internet
Author: User

Exploration of string memory management policies in iOS: ARC & MRC
Two days ago, I argued with my colleagues about whether the copy operation would change after NSString was executed. After a long time, I wrote the code to verify that the NSString operation was not as simple as we thought, next let's take a look at what happened to NSString and NSMutableString executing retain, copy, mutableCopy in MRC, and different modifier _ weak, _ strong modifier assignment under ARC. I. Verification Code: copy the code-(void) testStringAddress {int a = 0; int B = 0; static int c = 0; NSString * str = @ "Hello World "; # if _ has_feature (objc_arc) _ weak NSString * weakStr = str; _ strong NSString * strongStr = str; # else NSString * retainStr = [str retain]; # endif NSString * copyStr = [str copy]; NSMutableString * mutableCopyStr = [str mutableCopy]; // verify whether mutableCopy is a mutableString. If it is not executed, Crash [mutable CopyStr appendFormat :@".. "]; str = @" I'm changed "; NSString * str2 = [NSString stringWithFormat: @" Hello world "]; # if _ has_feature (objc_arc) _ weak NSString * weakStr2 = str2; _ strong NSString * strongStr2 = str2; # else NSString * reta1_2 = [str2 retain]; # endif NSString * copyStr2 = [str2 copy]; NSString * copy2Str2 = [str2 copy]; NSString * mutableCopyStr2 = [str2 mutableCopy]; NSString * mutableCopy2 Str2 = [str mutableCopy]; str2 = [[NSString alloc] initWithFormat: @ "changed"]; NSMutableString * mutableStr = [NSMutableString stringWithString: @ "hello world"]; # if _ has_feature (objc_arc) _ weak NSMutableString * weakMutableStr = mutableStr; _ strong NSMutableString * Signature = mutableStr; # else NSMutableString * Signature = [mutableStr retain]; # endif NSMutableString * copyMutableStr = [MutableStr copy]; NSMutableString * copy2MutableStr = [mutableStr copy]; NSString * mutableCopyMutableStr = [mutableStr mutableCopy]; NSString * 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, mutable CopyStr); 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 ret AinStr: % @, \ r copyStr: % @, \ r mutableCopyStr: % @ ", str, retainStr, copyStr, mutableCopyStr); NSLog (@" \ r str2: % @, \ r retainStr2: % @, \ r copyStr2: % @, \ r mutableCopyStr2: % @ ", str2, reta1_2, copyStr2, mutableCopyStr2); NSLog (@" \ r mutableStr: % @, \ r retainMutableStr :%@, \ r copyMutableStr :%@, \ r mutableCopyMutableStr :%@ ", mutableStr, retainMutableStr, copyMutableStr, mutableCopyMutableStr); # endif} In the Code, two int variables are defined at the beginning, mainly to print the stack address of the current function. By the way, it is verified that the stack in the memory grows from the high address to the low address. Use the pre-compiled macro # if _ has_feature (objc_arc) to check whether the current environment is an ARC environment, and write different test code according to different environments. By adding a breakpoint at the end of testStringAddress, run the "p" command on the lldb command line to output the address corresponding to the variable. For example, you can view the memory address and content pointed to by str, enter "p str. Debugging environment xCode6 beta4 and iOS8 SDK. 2. The execution result in MRC is shown in: 1. The first part "p & str" indicates printing the address of the str variable, "p str" indicates the address of the content pointed to by str. As shown in, the output of "p & str" is 0xbff0aff8, and the address of local variable B is 0xbff0affc. We can see that the memory addresses of both are connected, because they are all local variables in the function body. The output of "p str" is 0x000a7048, indicating that the storage region of the content pointed to by str is different from the local variable of the function. 2. Part 2 c is a static variable defined in the function. The address printed by "p & c" is 0x000a778c. We can see that the variable c and the function variable are also different. Only one static variable exists in the static variable area of the program while the program is running. 3. The third part of str is defined as "NSString * str = @" Hello World ";". After debugging, it is found that str points to 0x000a7048, in this case, the result address of the retain and copy operations on str is 0x000a7028. The NSString address obtained after mutableCopy is 0x7974a110, which is different from the address obtained by str, retain, and copy operations. Summary: variables in the form of @ "Hello World" are in the constant area of the program, while NSString creates a new object when executing retain and copy for objects in the constant area for the first time, then execute similar operations to take advantage of the previously created objects. The mutableCopy operation creates an object elsewhere and completes initialization using str. 4. In Part 4, str2 is defined as "NSString * str2 = [NSString stringWithFormat: @" Hello world "];". The address of str2 pointing to is 0x79749000, the address of the string pointing to the content obtained by the retain operation is 0x7974a5c0, and the copy operation is also 0x7974a5c0. After the test, the copy operation on str2 will get the same address. This seems to be different from the concept of "retain adds reference technology and copy creates new objects" that we often read. Execute mutableCopy on str2 to get the address 0x7974a380 and recreate an object, which meets our expectation. Summary: NSString objects created using stringWithFormat are on the stack by default. A copy is created when retain or copy operations are performed on these objects for the first time, all subsequent retain and copy will point to the same copy created previously. A new object will be created whenever the mutableCopy operation is executed. 5. The fifth part of mutableStr is defined as "[NSMutableString stringWithString: @" hello world "];". The output shows that the address of the object is the same as that of mutableStr after performing the retain operation on mutableStr, executing the copy operation will create a new object, and performing the mutableCopy operation will also create a new object. Summary: NSMutableString objects created using stringWithString are on the stack by default. When retain is executed on NSMutableString, no new objects are created. When copy and mutableCopy are executed, new objects are created. The above object addresses can be roughly divided into four intervals: 0x000a70xx, 0x000a78xx, 0x7974xxxx, and 0xbff5xxxx. In fact, they represent four different memory segment constant zones, static variable zones, and heap zones respectively, stack area.

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.