AutoReleasePool is an automatic memory reclamation mechanism in oc. In MRC, you can use AutoReleasePool to delay the release. In ARC, you can use AutoReleasePool to add objects to the nearest AutoReleasePool. Runloop is not released until it goes to sleep or is out of AutoReleasePool{} scope
Autoreleasepool {} is a c++ structure with a push constructor and a pop destructor
Official document Description:
An automatic release pool is a stack structure with Pointers to free objects or pool_boundary. An automatic release pool is a page structure (similar to virtual memory structure). It is a bidirectional linked list with parent nodes and byte points
AutoReleasePoolPage
objc_aotureleasePoolPush->autoreleasePage::push()
AutoReleasePool is both a page and an object. The page size is 4096. AutoreleasePage inherits autoreleasePageData. Next Object depth hiwat: indicates the maximum number of stacks
AutoReleasePoolPush
AutoReleasePoolPush: Determines whether a pool exists first. If not, the pool is created using the AutoreleasenewPool: method. If so, the sentinel object is pushed using the Autoreleasefast method
Autoreleasenewpool: The autoreleaseFullpage(obj, Page) object is pushed if there is a current thread operation page hotPage (), and the page is created using AutoReleasenopage if there is not
Autoreleasenopage: the current page does not exist -> Create an automatic releasepool for the current thread using AutoreleasepoolPage (autoreleasenopageData initializer)
Autoreleasepoolpage constructor {begin() where to press (the next object to be released) the top of the page + 56 56 is the size of the autoreleasepoolPageData structure objc_thread_self() Current thread newparent parent depth Parent depth hiwat Maximum number of pushes}
View automatic free pool memory structure
A page has 4096 bytes, and the auto-release pool itself occupies 56 bytes, so a page can actually store 4040 bytes, and an object pointer 8 bytes, so a total of 505 objects can be stored, but the first page has another sentinel object, so the first page can only store 504 objects
Autoreleasepool is a structure object that is a stack structure for a bidirectional linked list of multiple pages, first in, last out
The autoreleaseFast object is pressed
Hotpage (); paga->add(obj); autoreleaseFullPage(obj); autoreleaseNoPage(obj);
autoreleaseFullPage
In essence, a do while loop queries byte points if they are currently full, creates a new page autoreleasepoolPage (Page) if none exists and pushes the current page, and terminates the loop as the current page pushes the current page if they are not full
add(obj)
Press the object to the position of the next pointer on the path, and then ++ next, where the next object is stored, through the stack structure
AutoReleasePoolPop analysis
If there is a page, fault tolerance is first performed, and then popPage is called out of the stack. In this method, release messages are sent to the stack until all objects are released from the current page stop position until the incoming sentry object is encountered
Releaseuntil out of the stack
First, release the object by getting the next page (the last object in the page), and decrement next to get the last object, whether it is a sentinel object, if it is not the sentinel object will automatically call objc_release to release
kill
Destroys the current page, assigns the current page to the parent page, and sets the child object pointer to nil for the parent page
conclusion
In automatic release pool pressing, when there is no pool, that is, there is only empty placeholder, create a page, push the sentinel object, when the page is pressing ordinary objects, mainly through the next pointer decrement, when the stack is full, the child object that needs to set the page is the new page, the process is as follows:
When a page is empty, the parent object that needs to be assigned to the page continues to be pushed until stop is found and not the sentry, and the empty current child page is deleted and the parent page is set to the current page