How ARC works

I have learned something about ARC and MRC before, you can see MRC
ARC


这篇记录一下在运行期和编译器ARC都干了什么

what did you do

chat: In computer science, ARC (Automatic Reference Counting) is a memory management technique used to track and manage the reference count of an object so that the memory occupied by the object is released when it is no longer needed.
At runtime, ARC is responsible for keeping track of the object's reference count. The reference count is incremented whenever there is a new reference to the object and decremented when the reference ends. When the reference count is zero, it means that the object is no longer referenced by any code, and the memory occupied by the object can be safely released. Therefore, ARC monitors and manages the life cycle of objects at runtime.
During compilation, ARC is automatically generated and inserted by the compiler to maintain the reference count. Specifically, the compiler analyzes the code and inserts operations such as retain, release, and autorelease at appropriate locations to increase or decrease the reference count of the object. This way, developers can concentrate on business logic without manually managing memory.
ARC simplifies the process of memory management and reduces the errors and workload of manual memory management through the combination of runtime and compile-time. It provides an automatic memory management mechanism, enabling developers to focus more on the implementation of application functions, improving development efficiency and code quality

  • In addition to automatically calling the "retain" and "release" methods, using ARC has the added benefit of performing optimizations that would be difficult or impossible to do manually.
  • ARC will simplify the retain, release, and autorelease operations that can offset each other. ARC can sometimes remove the two operations in pairs if it finds multiple retain and release operations on the same object. ARC will analyze the lifetime requirements of the object, and automatically insert the code called by the appropriate memory management method at compile time, without requiring you to remember when to use the retain, release, and autorelease methods. The compiler will also generate the appropriate dealloc method for you.
  • Leaving memory management to the compiler and runtime components allows for multiple optimizations in the code. For example, ARC can detect the redundant operation of autorelease followed by retain at runtime. To optimize the code, when returning an autoreleased object in a method, a special function is executed.

How should I treat ARC? Where does it put the code that retains/releases calls?

Try not to think about where ARC puts the code that retains/releases calls, but instead think about the application algorithm, think about the object's strong and weak pointers, ownership, and possible circular references.

Do I still need to write a dealloc method for my object?

Sometimes it is necessary. Because ARC doesn't automatically handle malloc/free, lifecycle management of Core Foundation objects, file descriptors, etc., you can still free these resources by writing a dealloc method. 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 using ARC. [super dealloc] is not required and not allowed in the dealloc method under ARC, and the Runtime will handle it automatically.

Are circular references still possible in ARC?

Yes, ARC automatically retain/release, also inherited the circular reference problem. Fortunately, code migrating to ARC rarely starts leaking because properties are declared whether to retain or not.

How do blocks work in ARC?

Under ARC, the compiler will automatically copy the block on the stack to the heap according to the situation, such as when the block is used as a function return value, so you don't have to call Block Copy.
One thing to note is that under ARC, if NSString * __block myString is written like this, the block will have a strong reference to the NSString object instead of causing a dangling pointer problem. If you want to be consistent with MRC, use __block NSString * __unsafe_unretained myString or (better yet) __block NSString * __weak myString .

Is ARC slow?

No. The compiler effectively eliminates many extraneous retain/release calls, and a lot of effort has been put into speeding up the Objective-C runtime. In particular, the common "return a retain/autoreleased object" pattern is much faster when the caller of the method is ARC code, and doesn't actually put the object in the autorelease pool.

working principle

Guess you like

Origin blog.csdn.net/weixin_61196797/article/details/131667429