search

Home  >  Q&A  >  body text

objective-c - iOS内存管理【ARC、MRC、内存池】三者的关系?

1.iOS内存管理有三种方法:ARC、MRC、内存池。
2.MRC:遵循谁申请、谁添加、谁释放的原则。需要手动处理内存技术的增加和修改。从12年iOS开始开始,逐步被ARC(自动内存计数)模式取代。
3.ARC就是取代了MRC,在App编译阶段,由Xcode添加了内存管理的代码。
4.内存释放池Release Pool:把需要释放的内存统一放在一个池子中,当池子被抽干后(drain),池子中所有的内存空间也被自动释放掉。 内存池的释放操作分为自动和手动。自动释放受runloop机制影响。

5.内存池是属于MRC的技术吗?ARC模式下有自动内存计数了,MRC下有手动内存处理了。那么内存池是MRC下的相对于release 方法的另一种内存计数处理方法吗?

6.我是搞不清ARC、MRC、内存池 三者的关系。

7.ARC的语义就是在实际开发中,根本不用操心内存管理问题。(是吗?)

PHP中文网PHP中文网2757 days ago856

reply all(4)I'll reply

  • PHPz

    PHPz2017-05-02 09:27:59

    The effect that

    ARCMRC其实都是原始的内存管理方式,申请:alloc;释放:release,这种方式下内存的申请和释放都需要精确计算,对于比较复杂的程序,计算申请对象应该何时被释放是有些头昏脑热的事情。于是就出现了引用计数Reference Counting这种方式,用来统计对象的使用情况以得到对象应不应该被释放。
    MRC就是Mannul Reference Counting,它需要手动进行对象使用的计算,也就是每次使用对象时,要手动的retain一下给引用计数加一,而不再使用时还要release一下给引用计数减一。这样写久了,会感觉特别麻烦,要费老大劲去写retainrelease,于是乎就有了ARC自动引用计数,原理也很简单,就是通过程序去推断retainrelease应该出现的位置,代替我们去写retainrelease
    AutoRelease则又是完全不同的一套思路,有点儿像Java中的GC垃圾清理机制,它不会像ARC或者MRC那样在对象不再会被使用时马上被释放,而是等到一个时机去释放它。在ARC比较成熟的今天,用到的机会已经不多了,不过再一些特殊的场景下,AutoRelease还是能达到ARC cannot achieve.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-02 09:27:59

    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.

    reply
    0
  • 黄舟

    黄舟2017-05-02 09:27:59

    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?

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-02 09:27:59

    1. MRC In the earliest days, programmers managed memory manually.

    2. 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.

    3. 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.

    reply
    0
  • Cancelreply