We all know:
- Objects created by methods that start with alloc, new, copy, and mutableCopy will be released where appropriate, while objects created by other class method names such as [Test Object], [NSArray Array], [NSString stringWithFormat:] is not tagGedPointer, the system will append the autoRelease and may delay the release, May also be called Objc_retainAutoreleasedReturnValue and Objc_autoreleaseReturnValue combination optimization without delay the release
- For the main thread system autoreleasePool, pop is done in the beforeWaiting of runloop to send release messages. The page itself is the stack structure, and there is a bidirectional list between the pages. Pool nested with POOL_BOUNDARY sharing not full page and splitting with POOL_BOUNDARY (yup ~ beforeWaiting is a busy time dealing with UI etc)
Does the child thread have autoreleasePool?
If the child thread is empty:
A child thread may delay releasing an object:
######## POOL is equivalent to POOL_BOUNDARY
[Test Object] and [NSArray Array] do not need to be created manually. There is no delay in releasing the autoreleasePool. The non-taggedPointer object for [NSString stringWithFormat:] was released late
When is the autoreleasePool of the child thread destroyed?
Child threads do not enable Runloop by default, so they are destroyed differently from the main thread
Listen for a variable assignment to view the stack, [NSThread exit] -> [pthread exit] -> [_pthread_tsd_cleanup] -> [TLS dealloc] -> objc_autoreleasePoolpop -> The object’s dealloc
The child Thread pool is destroyed from TLS, which is the Thread Local Storage, when the Thread is destroyed
If a thread needs to be kept alive, it may be unable to be released, with predictable consequences…
Page destroys the source code details
In the previous demo, I noticed that sometimes there would be an empty page in the pool. I didn’t care about it until I read the source code:
The page is delayed destruction, destroying the child after less than half of the objects in the current page, presumably to avoid wasting performance by frequently creating destruction pages near the threshold
To validate the
Similarly, JDK1.8’s HashMap will switch between a linked list and a red-black tree in case of hash collisions. The 8-hour linked list will turn into a red-black tree, and the 6-hour red-black tree will turn into a linked list, also to avoid frequent switching around the threshold
If you think about it, apple has a lot more details like managing reference counting in ISA and SideTables, and switching weak references between fixed-length arrays and hash tables
About Objc_retainAutoreleasedReturnValue and Objc_autoreleaseReturnValue
Objective-c Advanced Programming iOS and OS X Multithreading and memory management
From the apple documentation:
Description in the LLVM documentation:
The system will try its best to optimize, and there is no fixed rule. The logic of the main thread and child thread is different, as is the logic of iOS and MacOS. The number of Autorelease objects will also affect the result
One final tip
A child thread has a pool by default, but either the main thread or the child thread has a large number of temporary variables (which may be released late) and it is best to write a pool by hand. This will pop at the end of the pool’s scope to avoid memory inflation or leaks
Don’t create too many deferred release objects in viewDidLoad, you’ll find that viewDidAppear will be called late
NSLog printing is sometimes toxic pit, try to debug breakpoints