1. There are three methods of iOS memory management: ARC, MRC, and memory pool.
2.MRC: Follow the principle of who applies, who adds, who releases. Additions and modifications to memory technology require manual handling. Starting from iOS in 2012, it has been gradually replaced by ARC (automatic memory counting) mode.
3.ARC replaces MRC. During the App compilation phase, Xcode adds memory management code.
4. Memory release pool Release Pool: Put the memory that needs to be released in a pool. When the pool is drained, all the memory space in the pool will be automatically released. The release operation of the memory pool is divided into automatic and manual. Automatic release is affected by the runloop mechanism.
5. Is the memory pool a technology of MRC? There is automatic memory counting in ARC mode, and manual memory processing in MRC. So is the memory pool another memory counting processing method under MRC relative to the release method?
6. I can’t figure out the relationship between ARC, MRC and memory pool.
7. The semantics of ARC is that in actual development, there is no need to worry about memory management issues at all. (Really?)
The effect that
ARC
和MRC
其实都是原始的内存管理方式,申请:alloc
;释放:release
,这种方式下内存的申请和释放都需要精确计算,对于比较复杂的程序,计算申请对象应该何时被释放是有些头昏脑热的事情。于是就出现了引用计数Reference Counting
这种方式,用来统计对象的使用情况以得到对象应不应该被释放。MRC
就是Mannul Reference Counting
,它需要手动进行对象使用的计算,也就是每次使用对象时,要手动的retain
一下给引用计数加一,而不再使用时还要release
一下给引用计数减一。这样写久了,会感觉特别麻烦,要费老大劲去写retain
和release
,于是乎就有了ARC
自动引用计数,原理也很简单,就是通过程序去推断retain
和release
应该出现的位置,代替我们去写retain
和release
。而
AutoRelease
则又是完全不同的一套思路,有点儿像Java
中的GC
垃圾清理机制,它不会像ARC
或者MRC
那样在对象不再会被使用时马上被释放,而是等到一个时机去释放它。在ARC
比较成熟的今天,用到的机会已经不多了,不过再一些特殊的场景下,AutoRelease
还是能达到ARC
cannot achieve.You have explained it very clearly. It cannot be said that ARC does not need to consider memory management at all, but it is indeed very complete. The memory pool is where you store the memory you apply for. This is easy to understand. I remember that in the case of ARC, it seems that the memory that previously needed to be released manually can be directly put into the release pool, and the system will automatically release the memory in the release pool. MRC is what you said, the principle of release based on who applies. Speaking of which, I still like MRC, it feels like I have a lot of power.
If we want to talk about history, oc has been MRC since its launch, and AutoRelease is a feature of 2.0. But I don’t know that autorelease is also related to runloop. I know that runloop is used in main function. Can I solve it?
MRC In the earliest days, programmers managed memory manually.
ARC was formerly the "Clang static analyzer" project, which was used to analyze Objective-C code to find errors such as memory leaks and premature release. The results were so good that Apple considered using this analyzer to automatically Insert all the retain and release, and finally led to the creation of ARC.
The Autorelease Pool should be something at the same time as MRC and has nothing to do with ARC. Common usage scenarios are: a method needs to return a newly created object, but you don’t want to retain it, and you are worried about early release, so you send an autorelease message to let the autorelease pool become its temporary owner and prevent it from being released early. When draining This temporary count is also subtracted.
So the name of the auto-release pool may be a bit misleading. The auto-release pool does not "automatically" release anything. What it releases is what you told it to release before.
To summarize: MRC is where programmers write retain and release to manage memory. The automatic release pool mechanism is used to solve some difficult object ownership issues. ARC saves all people from fire and water.
The execution result of ARC is that the code after retain and release is automatically added, which is no different from what you wrote in MRC, but it is less error-prone. In other words, the ARC mechanism is completely different from Java's garbage collection. ARC is a compile-time mechanism, not a run-time mechanism.
You still need to pay attention to memory issues when using ARC, such as strong reference cycles that are easy to occur when using blocks. ARC cannot help you with this kind of problem. It is relatively basic. You should find an Objective-C book that will cover it.