当前位置:网站首页>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 thestrong
andweak
pointers, ownership, and possible circular references. - Do I still need to write a
dealloc
method 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 writedealloc
method 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.Thedealloc
method 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
/release
also inherits the circular reference problem.Fortunately, code migrating to ARC rarely starts leaking because the property already declares whether or not toretain
. How does block
work in ARC?Under ARC, the compiler will automatically copy the
block
on the stack to the heap according to the situation, such as whenblock
is 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 myString
is written like this,block
will be strong on theNSString
objectreference instead of causing the dangling pointer problem.If you want to be consistent with MRC, use__block NSString * __unsafe_unretained myString
or (better yet) use__block NSString * __weak myString
.- Is ARC slow?
No.The compiler has effectively eliminated many extraneous
retain
/release
calls, 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.
边栏推荐
- 一个 15 年 SAP ABAP 开发人员分享的 SAPGUI 一些个性化设置和实用小技巧试读版
- 主流的深度学习推理架构有哪些呢?
- 大批程序员可能面临被劝退!
- Metaverse Web 3.0 和 DeFi大师班
- FastJson反序列化漏洞(复现)
- MySQL中的存储过程(详细篇)
- windwons 下GPU环境和pytorch安装
- Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
- 知识蒸馏2:目标检测中的知识蒸馏
- 一个 15 年 SAP ABAP 开发人员分享的 SAPGUI 一些个性化设置和实用小技巧
猜你喜欢
Test the.net text to Speech module System. Researched
bean的生命周期
Insert data into MySQL in C language
Error EPERM operation not permitted, mkdir 'Dsoftwarenodejsnode_cache_cacach Two solutions
【综合类型第 34 篇】喜讯!喜讯!!喜讯!!!,我在 CSDN 的第一个实体铭牌
Web 3.0入门教程
Mongoose module
莫队--优雅的暴力
Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)
习题:变量、常量和基本数据类型
随机推荐
超声波探伤仪是做什么用的?
JMeter Notes 4 | JMeter Interface Introduction
如何让 JOIN 跑得更快?
【云商店公告】关于7月30日帮助中心更新通知
想要写出好的测试用例,先要学会测试设计
shell快速移植
bean的生命周期
Mysql brush dirty several scenarios and related parameters
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
JMeter笔记3 | JMeter安装和环境说明
Py程序员的七夕情人节
952. 按公因数计算最大组件大小 : 枚举质因数 + 并查集运用题
数据库系统原理与应用教程(068)—— MySQL 练习题:操作题 90-94(十二):DML 语句练习
Analysis and Simulation of Short Circuit Fault in Power System Based on MATLAB
web服务通过用户访问请求判断设备来源
C陷阱与缺陷 第7章 可移植性缺陷 7.3 整数的大小
BCryptPasswordEncoder的使用及原理
What is industrial radiography equipment?
论文阅读之《Color Constancy Using CNNs》
windwons 下GPU环境和pytorch安装