当前位置:网站首页>Autoreleasepool problem summary
Autoreleasepool problem summary
2022-07-28 05:06:00 【Lu_ Ca】
AutoreleasepoolPage
call autorelease The object eventually passes through AutoreleasepoolPage Objects to manage .AutoreleasepoolPage The structure of the object is as follows
Next: Point to the latest autorelease The next position of the object , That is, the first available location .
thread: Current thread .
parent: Point to parent node , Of the first node parent yes nil.
Child: Point to the child node , Of the last node child Point to nil.
Depth: Expressing depth .
magic: Used to verify AutoreleasepoolPage Whether the structure is complete .
Hiwat: Upper limit of data container
Every autoreleasepoolpage Object occupancy 4096 Byte memory , In addition to storing internal member variables , The rest is stored autorelease Address of the object .autoreleasepoolpage The object is through Double linked list Connected together in the form of .
push and pop Method
When an object calls [object autorelease] Function will add the object to autoreleasepool in , That is to say, it will go on push operation .
push operation , Will be called to autoreleaseFast function . This is its initialization function . By calling hotpage() function , Judge that there are three situations :
1、 If hotpage There is , And the capacity is not full , Then call directly add Function to add an object .
2、 If hotpage There is , But it's full , call autoreleaseFullPage function , And initialize a AutoreleasePage Marked as hotpage.
3、 If hotpage non-existent , call autoreleaseNoPage function , Create a AutoreleasePage Marked as hotpage, Add sentinel object .
Autoreleasepool Of pop The operation is mainly
1、 Get the location through the sentinel object autoreleasepoolpage
2、 call page->releaseUntil(stop) function , To the top of the stack stop Objects call objc_release() Operation reference count -1
3、 Last call child Of kill Method .
autoreleasepool Creation and release time of
establish
App After starting , Main thread runloop Two will be added in observer, The first is surveillance loop Of entry event , Within the callback event autoreleaspoolpush Create an automatic release pool . And set this observer Of order The highest priority ( Negative 2 Of 32 Power -1), Ensure that the automatic release pool is created before other callbacks .
Release
the second observer Monitoring is to prepare for hibernation and about to exit runloop, It calls autoreleasepoolpop Release the automatic release pool , Its observer Of order Set the lowest priority (2 Of 32 Power -1), It ensures that the automatic release pool is released after other callbacks . It prevents the automatic release pool from releasing objects prematurely and causing crashes .
Object release timing
mrc Next
Manual call autorelease Method , The release timing is not determined by the scope of the object , But according to runloop Different states of , When runloop The system will be cleaned up at the end autorelease Objects to be processed . and runloop The end is not fixed duration.
arc Next
Local objects are destroyed after they are out of scope , When the system is out of scope , Call it automatically release Method .
autoreleasepool Use scenarios
Official documents indicate that there are three scenarios :
1、 When writing a command line program , No, UI Framework procedures . because autorelease The mechanism is based on UI framework, So if it's not based on UI framework Your program needs to manage the life cycle of the object itself .
2、 Write a cycle , It contains a large number of temporarily created objects . because autorelease The trigger time is next runloop When , So if there are a lot of autorelease object , Then next time runloop There is no chance to be released before coming back , Will cause memory explosion , May cause crash problems .
3、 Create your own thread .
What objects will be added to Autoreleasepool in
1、 Use alloc、copy、new Etc , System management object memory .
2、 Use __weak Decorated object , To ensure that references are not discarded , Will register to Autoreleasepool in .
3、id Pointer to or pointer to an object , It will be registered to Autoreleasepool in .
边栏推荐
- Introduction to testcafe
- The go zero singleton service uses generics to simplify the registration of handler routes
- How to simulate common web application operations when using testcafe
- RT_ Use of thread message queue
- Testcafe provides automatic waiting mechanism and live operation mode
- Simulink automatically generates STM32 code details
- 【内功心法】——函数栈帧的创建和销毁(C实现)
- HDU 2874 connections between cities
- [Hongke technology] Application of network Multimeter in data center
- Online sql to XML tool
猜你喜欢

基于MPLS构建虚拟专网的配置实验

C语言ATM自动取款机系统项目的设计与开发

为什么md5不可逆,却还可能被md5免费解密网站解密

MySQL(5)

HashSet add

100 lectures on Excel practical application cases (XI) - tips for inserting pictures in Excel

【ARXIV2204】Simple Baselines for Image Restoration

猿辅导技术进化论:助力教与学 构想未来学校

Comprehensively analyze the differences between steam and maker Education

After easycvr is connected to the national standard equipment, how to solve the problem that the equipment video cannot be played completely?
随机推荐
微服务故障模式与构建弹性系统
多御安全浏览器将改进安全模式,让用户浏览更安全
After a year of unemployment, I learned to do cross-border e-commerce and earned 520000. Only then did I know that going to work really delayed making money!
Redux basic syntax
(3.1) [Trojan horse synthesis technology]
[high CPU consumption] software_ reporter_ tool.exe
Know etcd
MySQL 默认隔离级别是RR,为什么阿里等大厂会改成RC?
你必需要了解的saas架构设计?
How to send and receive reports through outlook in FastReport VCL?
【ARXIV2204】Vision Transformers for Single Image Dehazing
(2.4) [service Trojan -slimftp] introduction and use
HashSet add
Analyze the emotional elements contained in intelligent sweeping robot
FPGA: use PWM wave to control LED brightness
【CVPR2022】Multi-Scale High-Resolution Vision Transformer for Semantic Segmentation
CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?
POJ 3728 the merchant (online query + double LCA)
Test report don't step on the pit
阿里怎么用DDD来拆分微服务?