当前位置:网站首页>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 .
边栏推荐
- The research group passed the thesis defense successfully
- How to send and receive reports through outlook in FastReport VCL?
- Leetcode 18. sum of four numbers
- RT based_ Distributed wireless temperature monitoring system based on thread
- What tools do software testers need to know?
- How to simulate common web application operations when using testcafe
- HDU 1522 marriage is stable
- How to quickly locate bugs? How to write test cases?
- jsonp 单点登录 权限检验
- Service object creation and use
猜你喜欢

With a monthly salary of 15.5K, he failed to start a business and was heavily in debt. How did he reverse the trend through software testing?

【ARXIV2203】Efficient Long-Range Attention Network for Image Super-resolution

HashSet add

Leetcode 454. Adding four numbers II

After easycvr is connected to the national standard equipment, how to solve the problem that the equipment video cannot be played completely?

Redux basic syntax

What is the core value of testing?

RT_ Use of thread mailbox

Paper reading notes -- crop yield prediction using deep neural networks

MySQL 默认隔离级别是RR,为什么阿里等大厂会改成RC?
随机推荐
从微服务基本概念到核心组件-通过一个实例来讲解和分析
【ARIXV2204】Neighborhood attention transformer
为什么md5不可逆,却还可能被md5免费解密网站解密
HDU 3585 maximum shortest distance
【ARXIV2204】Simple Baselines for Image Restoration
FPGA: use PWM wave to control LED brightness
Keil Chinese garbled code solution
Read the paper -- a CNN RNN framework for clip yield prediction
在外包公司两年了,感觉快要废了
Service object creation and use
Method of converting UI file to py file
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!
Handling of web page image loading errors
【CVPR2022 oral】Balanced Multimodal Learning via On-the-fly Gradient Modulation
Redis type
驾驭EVM和XCM的强大功能,SubWallet如何赋能波卡和Moonbeam
How to analyze fans' interests?
How does Alibaba use DDD to split microservices?
使用nfpm制作rpm包
[paper notes] - low illumination image enhancement - zeroshot - rrdnet Network - 2020-icme