In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the most frequently used memory management in iOS". In daily operation, I believe many people have doubts about the memory management that iOS uses most frequently. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what are the most frequently used memory management in iOS?" Next, please follow the editor to study!
1. Block memory management 1.block memory type
There are three types of block memory:
_ NSConcreteGlobalBlock (global)
_ NSConcreteStackBlock (stack)
_ NSConcreteMallocBlock (heap)
two。 Timing for the creation of three types of memory
1) for _ NSConcreteStackBlock and _ NSConcreteGlobalBlock types
We can manually create two types of block, _ NSConcreteStackBlock and _ NSConcreteGlobalBlock, as follows:
Void (^ globalBlock) () = ^ {}; int main (int argc, const char * argv []) {@ autoreleasepool {void (^ stackBlock1) () = ^ {};} return 0;}
So how do we determine that these two block are the two types of block we are talking about? we can use clang-rewrite-objc xxx.m (clang-x objective-c-rewrite-objc-isysroot / Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk xxxxx.m) to compile and convert into the C++ implementation, and we can see the result of the conversion, as shown below:
/ / globalBlockstruct _ globalBlock_block_impl_0 {struct _ block_impl impl; struct _ globalBlock_block_desc_0* Desc; _ globalBlock_block_impl_0 (void * fp, struct _ globalBlock_block_desc_0* desc, int flags=0) {impl.isa = & _ NSConcreteGlobalBlock; impl.Flags = flags; impl.FuncPtr = fp; Desc = desc;}}; / / stackBlockstruct _ main_block_impl_0 {struct _ _ block_impl impl Struct _ _ main_block_desc_0* Desc; _ _ main_block_impl_0 (void * fp, struct _ _ main_block_desc_0* desc, int flags=0) {impl.isa = & _ NSConcreteStackBlock; impl.Flags = flags; impl.FuncPtr = fp; Desc = desc;}};. Int main (int argc, const char * argv []) {/ * @ autoreleasepool * / {_ _ AtAutoreleasePool _ autoreleasepool Void (* stackBlock) () = (void (*) ()) & _ _ main_block_impl_0 ((void *) _ main_block_func_0, & _ _ main_block_desc_0_DATA);} return 0;}
You can see that globalBlock is of type _ NSConcreteGlobalBlock, that is, it is created in the global zone, and the block variable is stored in the global data store; stackBlock is of type _ NSConcreteStackBlock, that is, created in the stack area.
2) for _ NSConcreteMallocBlock type
The memory of the NSConcreteMallocBlock type is obtained through the block copy of type _ NSConcreteStackBlock, so which types copy the block?
Block as the return value
/ / if it is a block of weak type, copy// _ _ weak void (^ weakBlock) () = ^ {I;}; / / ARC output / / full () {return page- > add (obj) {return page- > add (obj);} else if (page) {return autoreleaseFullPage (obj, page);} else {/ / call autoreleaseNoPage method to manage autorelease object automatically. Return autoreleaseNoPage (obj);}}
As a developer, it is very important to have a learning atmosphere and a communication circle. This is my iOS communication group: 519832104 whether you are rookie or Daniel, welcome to stay, share experiences, discuss technology, and share learning and growth together!
A copy of the interview questions collected by my friends is attached, which requires iOS to develop study materials and interview real questions. You can add iOS development advanced communication group and download it by yourself.
III. Memory management of weak objects
1. Release time
When dealloc, the value of the weak property is set to nil
two。 How to realize
Runtime maintains a weak table that stores all weak pointers to an object. For weak objects, it is placed in a hash table, where Key is the address of the object, and Value is the address of the weak pointer (the value of this address is the address of the object). When the reference count of this object is 0, it will dealloc. If the memory address of the object pointed to by weak is a, then it will search the weak table with an as the key, find all the weak objects with an as key, and set it to nil.
Note: since multiple weak pointers may point to the same object, value is an array
The implementation principle of weak can be summarized as follows:
1) during initialization: runtime calls the objc_initWeak function to initialize a new weak pointer to the address of the object.
Let's take the following line of code as an example:
Listing 1: sample code
{id _ _ weak obj1 = obj;}
When we initialize a weak variable, runtime calls the objc_initWeak function. This function is declared in Clang as follows:
Id objc_initWeak (id * object, id value)
The specific implementation is as follows:
Id objc_initWeak (id * object, id value) {* object = 0; return objc_storeWeak (object, value);}
The simulation code that rotates the sample code to the compiler is as follows:
Id obj1;objc_initWeak (& obj1, obj)
Therefore, what you do here is to initialize obj1 to 0 (nil), and then pass the address of obj1 and obj as arguments to the objc_storeWeak function.
The objc_initWeak function has a prerequisite: object must be a valid pointer that is not registered as a _ _ weak object. Value can be a null or point to a valid object.
2) when adding a reference: the objc_initWeak function calls the objc_storeWeak () function.
The function of objc_storeWeak () is to update the pointer to create a corresponding weak reference table.
3) when released, call the clearDeallocating function.
The clearDeallocating function first gets an array of all weak pointer addresses based on the object address, then iterates through the array to set the data to nil, finally removes the entry from the weak table, and finally cleans up the object's record.
Fourth, NSString memory manages the type of 1.NSString memory
There are two types of NSString memory:
_ _ NSCFConstantString (constant area)
_ _ NSCFString (heap area), NSTaggedPointerString (heap area)
two。 The timing of the creation of the two memory types.
There are three ways to generate a string of type NSString:
Method 1. Direct assignment:
NSString * str1 = @ "my string"
Method 2. Class function initialization generation:
NSString * str2 = [NSString stringWithString:@ "my string"]
Method 3. Instance method initialization generation:
NSString * str3 = [[NSString alloc] initWithString:@ "my string"]; NSString * str4 = [[NSString alloc] initWithFormat:@ "my string"]
1) for _ _ NSCFConstantString
This type of string is a constant string. This type of string is created literally, saved in the string constant area, and is created at compile time.
NSString * a = @ "str"; NSString * b = [[NSString alloc] init]; NSString * c = [[NSString alloc] initWithString:@ "str"]; NSString * d = [NSString stringWithString:@ "str"]; NSLog (@ "% @: class =% @", a NSLog NSStringFromClass ([a class])); NSLog (@ "% @: class =% @", bQuery NSStringFromClass ([b class])); NSLog (@ "% @: class =% @", Cigue NSStringFromClass ([c class])) NSLog (@ "% @: class =% @", dMagneNSStringFromClass ([d class])); / / print the result 2019-06-23 19class 13.240611 str 0800 BlockDemo [47229RV 789011] str: class = _ NSCFConstantString2019-06-23 1923NSStringFromClass 0800 BlockDemo [47229Rd 789011]: class = _ _ NSCFConstantString2019-06-23 19ZeR 13.240870mm 0800 BlockDemo [47229R789011] str: class = _ NSCFConstantString2019-06-23 19class 13,2409570800 BlockDemo [47229RH 789011] str: class = _ NSCFConstantString
2) for _ _ NSCFString and NSTaggedPointerString
_ _ NSCFString represents a string of object type, which is created at run time and saved in the heap area. The initial reference count is 1, and its memory management is the memory management of the object.
NSTaggedPointerString is an optimization of the _ _ NSCFString type. When running to create a string, it will judge the content and length of the string. If the content is made up of ASCII characters and the length is small (it is not clear how small it will be), the string type created at this time is NSTaggedPointerString.
For test results that cannot be changed to NSString:
NSString * e = [[NSString alloc] initWithFormat:@ "str"]; NSString * f = [NSString stringWithFormat:@ "str"]; NSString * g = [NSString stringWithFormat:@ "123456789"]; NSString * h = [NSString stringWithFormat:@ "1234567890"]; NSLog (@ "% @: class =% @", eRegent NSStringFromClass ([e class])); NSLog (@ "% @: NSStringFromClass ([f class])) NSLog (@ "% @: class =% @", NSLog NSStringFromClass ([g class])); NSLog (@ "% @: NSStringFromClass ([h class])) / / print the result 2019-06-23 19NSTaggedPointerString2019 27class: class = NSTaggedPointerString2019-06-23 1915 28614800 BlockDemo [48129WR 794364] str: class = NSTaggedPointerString2019-06-23 19NSTaggedPointerString2019 2719.115388280800 BlockDemo [48129Rd 794364] 123456789: class = NSTaggedPointerString2019-06-23 19VR 2719.115476140800 BlockDemo [48129Rd 794364] 1234567890: class = _ NSCFString
For variable NSMutableString
NSMutableString * ms1 = [[NSMutableString alloc] init]; NSMutableString * ms2 = [[NSMutableString alloc] initWithString:@ "str"]; NSMutableString * ms3 = [[NSMutableString alloc] initWithFormat:@ "str"]; NSMutableString * ms4 = [NSMutableString stringWithFormat:@ "str"]; NSMutableString * ms5 = [NSMutableString stringWithFormat:@ "123456789"]; NSMutableString * ms6 = [NSMutableString stringWithFormat:@ "1234567890"]; NSLog (@ "% @: class =% @", ms1,NSStringFromClass ([ms1 class])) NSLog (@ "% @: class =% @", ms2,NSStringFromClass ([ms2 class])); NSLog (@ "% @: class =% @", ms3,NSStringFromClass ([ms3 class])); NSLog (@ "% @: class =% @", ms4,NSStringFromClass ([ms4 class])); NSLog (@ "% @: class =% @", ms5,NSStringFromClass ([ms5 class])); NSLog (@ "% @: class =% @", ms6,NSStringFromClass ([ms6 class])) / / print the result 2019-06-23 1914 NSCFString2019 08.521931mm 0800 BlockDemo [49465191V 802590]: class = _ _ NSCFString2019-06-23 19mer 34mer 08.522058mm 0800 BlockDemo [49465RV 802590] str: class = _ NSCFString2019-06-23 191414R 0800 BlockDemo [49465Flt 802590] str: class = _ _ NSCFString2019-06-23 1949 34MU 08.52219680800 BlockDemo [49465dev 802590] str: class = _ _ NSCFString2019-06-23 19Get 34ther 08.522281140800 BlockDemo [4946514802590] 123456789: class = _ NSCFString2019-06-23 19 class 34 NSCFString 08.52237228 0800 BlockDemo [49465 BlockDemo 802590] 1234567890: class = _ _ NSCFString
From the results, we can see that the NSMutableString is allocated in the heap area and is of the type of _ _ NSCFString, and the Format-related methods in NSString are also allocated in the heap area, but they are divided into _ _ NSCFString and NSTaggedPointerString according to the length of the string. In fact, some of these variables in the allocation heap area are normal objects and some become autorelease objects. We can print out the specific ones using _ objc_autoreleasePoolPrint (), such as g, ms4, ms5, and ms6 in the instance.
At this point, the study on "which kinds of memory management are the most frequently used in iOS" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.