4

I was reading about memory management for various languages and the fact that Objective-C doesn't have managed memory because it causes stutters in the UX of the mobile apps.

I know ARC doesn't deal with circular reference, while Python's GC does.

I know that ARC is not a garbage collector at all and so it does not stop the execution of the main program to perform the memory management.

Could Python use a hybrid approach? Take some of the advantages of ARC and at the same time run the GC less often (or for a shorter period of time) so that it can still deal with circular references?

Or as anything like that being attempted within the Python community?

EDIT: I know Python uses reference counting, but as far as I know the objects whose reference drops to 0 are not immediately removed from memory (which ARC does). I am wondering if an immediate release of the memory occupied by that object could make Python more suitable in low memory environments, and because in that case the GC would possibly run less often, it would cause less threads interruptions. Both things would be ideal for a mobile application as far as UX.

4

1 回答 1

7

据我所知,Objective-C 的 ARC 最终给出了与 CPython 的引用计数相同的东西,但减去了循环引用处理。ARC 只是一个花哨的新名称,它被(重新)发明为意味着 Objective-C 编译器,而不是程序员,自动插入引用计数代码。

让我们为了这个答案的目的假设 CPython 可以用 Objective-C 重写(这是不现实的)。通过在显式引用计数上使用 ARC 你会得到:

  • 更简单的代码。

  • 没有循环收集。

  • 保证正确的代码。CPython 中的引用计数有一些已知的,可能还有一些未知的问题,在极不可能的情况下会导致崩溃;有些Lib/test/crashers/是基于此。

  • 代码稍慢。事实上,编译器在发现我们实际上不需要操作引用计数器的情况时通常不如人类聪明。

作为一名 PyPy 开发人员,我可以报告我自己的经验:我们过去曾尝试过这样做,即在编译期间自动插入引用计数代码。但是我们放弃了这种方法,因为如果没有高级优化,您的代码会变得非常慢。相反,我们现在使用一些简单的“真正的”垃圾收集(GC)系统;它甚至比 CPython 的引用计数提供更好的性能。所以我对编译器制造商的建议是,如果你想要一个可以自动插入必要代码的编译器,你最好使用许多更好的 GC 中的任何一个。然而,最后我检查了一下,LLVM 在这方面的支持仍然几乎为零。

于 2013-07-20T08:48:29.747 回答