当前位置:网站首页>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.
边栏推荐
- 编曲软件FL Studio中文版安装教程及切换语言教程
- How Google earth engine realizes the arrangement and selection of our time list
- 有效的括号字符串[贪心练习]
- 数据库系统原理与应用教程(064)—— MySQL 练习题:操作题 51-61(八):查询条件的构造、通配符
- 大批程序员可能面临被劝退!
- C陷阱与缺陷 第7章 可移植性缺陷 7.3 整数的大小
- 【Cloud Store Announcement】Notice of Help Center Update on July 30
- Mysql brush dirty several scenarios and related parameters
- (17)[系统调用]追踪系统调用(0环)
- 数据库系统原理与应用教程(065)—— MySQL 练习题:操作题 62-70(九):分组查询与子查询
猜你喜欢
随机推荐
游戏化产品搭建思路的拆解与探究
C陷阱与缺陷 第7章 可移植性缺陷 7.1 应对C语言标准变更
查询表中开始日期与结束日期
Py程序员的七夕情人节
Promise入门到精通(1.5w字详解)
FP6606CMP5 CPC-16L USB类型-C和PD充电控制器 百盛电子代理商
weiit新零售小程序如何探索数字化门店的破局之路
Moralis去中心化Web3应用开发教程
What is an ultrasonic flaw detector used for?
Mathematical Principles of Graph Convolutional Neural Networks——A Preliminary Study on Spectral Graph Theory and Fourier Transform
习题:变量、常量和基本数据类型
MySQL中的存储过程(详细篇)
MySQL【单行函数】
fast shell porting
有效的括号字符串[贪心练习]
图卷积神经网络的数学原理——谱图理论和傅里叶变换初探
Test the.net text to Speech module System. Researched
Weka 3.8.6安装与Weka 3.8.6功能介绍
JMeter笔记3 | JMeter安装和环境说明
LayaBox---TypeScript---类型兼容性









