当前位置:网站首页>What ARC does at compile time and runtime
What ARC does at compile time and runtime
2022-07-30 17:47:00 【Billy Miracle】
In addition to automatically calling the "retain" and "release" methods, there are other benefits of using ARC, which can perform optimizations that are difficult or impossible to do manually.
At compile time, ARC will reduce the operations of retain, release, autorelease that can cancel each other.If it finds that multiple "retain" and "release" operations are performed on the same object, then ARC can sometimes remove the two operations in pairs.ARC will analyze the object's lifetime requirements and automatically insert code for appropriate memory management method calls at compile time, without requiring you to remember when to use retain, release, autorelease method.The compiler will also generate the appropriate dealloc methods for you.
Delegating memory management to the compiler and run-time components allows for a variety of code optimizations.For example, ARC can detect at runtime the redundant pair of autorelease followed by retain.To optimize the code, a special function is executed when an autoreleased object is returned in a method.
Some Q&A
- How should I view ARC?Where does it put the code called by
retains/releases?Try not to think about where ARC puts the code called by
retains/releases, but think about the application algorithm, think about thestrongandweakpointers, ownership, and possible circular references. - Do I still need to write a
deallocmethod for my object?Sometimes needed.Because ARC does not automatically handle
malloc/free, lifecycle management of Core Foundation objects, file descriptors, etc., you can still writedeallocmethod to release these resources.You don't have to (actually can't) free instance variables, but you may need to call[self setDelegate:nil]on system classes and other code not written with ARC.Thedeallocmethod under ARC does not need and is not allowed to call[super dealloc], and Runtime will handle it automatically. - Are circular references still possible in ARC?
Yes, ARC automatically
retain/releasealso inherits the circular reference problem.Fortunately, code migrating to ARC rarely starts leaking because the property already declares whether or not toretain. How does blockwork in ARC?Under ARC, the compiler will automatically copy the
blockon the stack to the heap according to the situation, such as whenblockis used as the return value of the function, so you don't have toInvokeBlock Copy.
One thing to note is that under ARC,NSString * __block myStringis written like this,blockwill be strong on theNSStringobjectreference instead of causing the dangling pointer problem.If you want to be consistent with MRC, use__block NSString * __unsafe_unretained myStringor (better yet) use__block NSString * __weak myString.- Is ARC slow?
No.The compiler has effectively eliminated many extraneous
retain/releasecalls, and has put a lot of effort into speeding up the Objective-C runtime.In particular, when the caller of the method is ARC code, the common "return a retain/autoreleased object" pattern is much faster and doesn't actually put the object into the autoreleased pool.
边栏推荐
- 游戏化产品搭建思路的拆解与探究
- C# 跨程序传图(共享内存块传图)跨exe传图
- 【综合类型第 34 篇】喜讯!喜讯!!喜讯!!!,我在 CSDN 的第一个实体铭牌
- leetcode-547:省份数量
- JMeter笔记3 | JMeter安装和环境说明
- Redis缓存穿透-热点缓存并发重建-缓存与数据库双写不一致-缓存雪崩
- 链表Oj练习题 纯C语言
- Mongoose module
- 如何让 JOIN 跑得更快?
- Mathematical Principles of Graph Convolutional Neural Networks——A Preliminary Study on Spectral Graph Theory and Fourier Transform
猜你喜欢
随机推荐
数据库系统原理与应用教程(064)—— MySQL 练习题:操作题 51-61(八):查询条件的构造、通配符
What is industrial radiography equipment?
Ecplise执行C语言报错:cannot open output file xxx.exe: Permission denied
Mathematical Principles of Graph Convolutional Neural Networks——A Preliminary Study on Spectral Graph Theory and Fourier Transform
想要写出好的测试用例,先要学会测试设计
向量检索基础方法总结
基于stm32的shell实现
C陷阱与缺陷 第7章 可移植性缺陷 7.1 应对C语言标准变更
Google earth engine如何实现我们时间列表的排列和选取
升级 MDK 5.37 后的问题处理: AC6编译选项, printf, 重启失效等
LeetCode 952. 按公因数计算最大组件大小
weiit新零售小程序如何探索数字化门店的破局之路
S7-200SMART中定时器的使用方法和常见注意事项汇总
17.机器学习系统的设计
信息学奥赛一本通 1915:【01NOIP普及组】最大公约数与最小公倍数 | 洛谷 P1029 [NOIP2001 普及组] 最大公约数和最小公倍数问题
数据库系统原理与应用教程(067)—— MySQL 练习题:操作题 82-89(十一):数据的增、删、改操作
论文阅读之《Underwater scene prior inspired deep underwater image and video Enhancement (UWCNN)》
分账系统二清解决方案如何助力电商平台合规经营?
Error EPERM operation not permitted, mkdir 'Dsoftwarenodejsnode_cache_cacach Two solutions
强烈推荐APP破解常用工具集合!









