1 It seems that everyone has considered the problem when learning iOS
So in this blog post I will explain in detail from ARC to iOS memory management, as well as Block-related principles and source codes. 2. Let’s start with ARC When talking about iOS memory management, we have to start with ARC (Automatic Reference Counting), which was introduced by WWDC2011 and iOS5. ARC is a feature of the LLVM 3.0 compiler that is used to automatically manage memory. Unlike GC in Java, ARC is a compiler feature rather than a runtime feature, so ARC actually automatically helps developers insert memory management code during the compilation phase rather than real-time monitoring and memory recovery. ARC's memory management rules can be summarized as follows:
3 What you need to know
GNUstep is also one of the GNU projects. It reimplements the Cocoa Objective-C software library as free software. In a sense, the implementation of GNUstep and Foundation frameworks is similar. Analyzing Foundation's memory management through GNUstep source code 4 Implementation of alloc retain release dealloc 4.1 GNU – alloc See the alloc function in GNUStep. GNUstep/modules/core/base/Source/NSObject.m alloc:
GNUstep/modules/core/base/Source/NSObject.m NSAllocateObject:
The NSAllocateObject function allocates the space required to store the object by calling the NSZoneCalloc function, then sets the memory space to nil, and finally returns a pointer to be used as the object. We simplify the above code: GNUstep/modules/core/base/Source/NSObject.m alloc simplified version:
The alloc class method uses the retained integer in struct obj_layout to save the reference count and writes it to the memory header of the object. It then returns after all memory blocks of the object are set to 0. The representation of an object is as follows: 4.2 GNU – retain GNUstep/modules/core/base/Source/NSObject.m retainCount:
GNUstep/modules/core/base/Source/NSObject.m retain:
In the above code, the NSIncrementExtraRefCount method first writes the code that causes an exception when the retained variable exceeds the maximum value (because retained is an NSUInteger variable), and then performs the retain++ code. 4.3 GNU – release Corresponding to retain, the release method does retain --. GNUstep/modules/core/base/Source/NSObject.m release
4.4 GNU – dealloc dealloc will release the object. GNUstep/modules/core/base/Source/NSObject.m dealloc:
4. ***pple implementation In Xcode, set Debug -> Debug Workflow -> Always Show Disassenbly to on. This way, you can see more detailed method calls after the breakpoint. By setting breakpoints on alloc and other methods of the NSObject class, you can see that several methods are called internally: retainCount
retain
release
You can see that they all call a common __CFdoExternRefOperation method. As you can see from the prefix, this method is included in Core Foundation and can be found in CFRuntime.c. The source code is simplified and listed as follows: CFRuntime.c __CFDoExternRefOperation:
Therefore, __CFDoExternRefOperation performs specific method calls for different operations. If op is OPERATION_retain, the method that specifically implements retain is removed. From the method name BasicHash, we can see that the reference count table is actually a hash table. The key is the hash (the address of the object) and the value is the reference count. The following figure compares Apple and GNU's implementation: 5 autorelease and autorelaesepool Apple's documentation for NSAutoreleasePool states: Each thread (including the main thread) maintains a stack of managed NSAutoreleasePools. When a new Pool is created, they are added to the top of the stack. When the Pool is destroyed, they are removed from the stack. The autorelease object will be added to the Pool at the top of the current thread's stack. When the Pool is destroyed, the objects in it will also be released. When the thread ends, all Pools are destroyed and released. Set breakpoints on the NSAutoreleasePool class method and the autorelease method, and view its running process. You can see that the following functions are called:
[NSAutoreleasePool showPools] can show the status of all pools of the current thread:
In objc4, you can see AutoreleasePoolPage:
AutoreleasePoolPage is composed in the form of a doubly linked list (corresponding to the parent pointer and child pointer in the structure). The thread pointer points to the current thread. Each AutoreleasePoolPage object will allocate 4096 bytes of memory (that is, the size of one page of virtual memory). In addition to the space occupied by the above instance variables, the remaining space is used to store the address of the autorelease object. The next pointer points to the location where the next autorelease object added will be stored. When the space of a Page is full, a new AutoreleasePoolPage object will be created to connect to the linked list. 6 __unsafe_unretained Sometimes we also use the __unsafe_unretained modifier in addition to __weak and __strong, so how much do we know about __unsafe_unretained? __unsafe_unretained is an unsafe ownership modifier. Although ARC memory management is the work of the compiler, variables with the __unsafe_unretained modifier do not belong to the compiler's memory management objects. Neither strong references nor weak references are obtained when assigning values. Let's run a piece of code:
Running results:
Detailed analysis of the code:
Therefore, the *** NSLog just happens to run normally. If it is accessed incorrectly, it will cause a crash. When using the __unsafe_unretained modifier, when assigning to a variable with the __strong modifier, make sure that the object actually exists. |
>>: Android View related core knowledge questions and answers
The mini program provides convenience for publici...
Become a technical person who can lead a team and...
Although 618 brings with it scarce and large traf...
When it comes to corporate Weibo operations , aft...
Why should you be an agent for WeChat Mini Progra...
Central Meteorological Observatory June 18, 06:00...
Community operation is a very long process that r...
Reviewer of this article: Chen Haixu, Deputy Dire...
Some time ago, the company was recruiting UI desi...
How much does it cost to attract investment throu...
If you keep thinking about it, there will be a re...
Five years after its launch, Google's self-de...
Competitive product analysis is one of the must-d...
"Dehumidification and sterilization", &...
A survey conducted by a travel platform showed th...