iOS 5有垃圾收集吗? [英] Does iOS 5 have garbage collection?

查看:84
本文介绍了iOS 5有垃圾收集吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否不再需要担心iOS 5以后的内存管理?是否所有为iOS 4及更早版本编写的程序都必须重写以允许iOS为您管理内存?

Do I no longer have to worry about memory management iOS 5 onwards? Also, will all programs written for iOS 4 and earlier versions have to be rewritten to allow iOS to manage the memory for you?

推荐答案

您似乎在谈论自动引用计数,在其他答案中提到。 ARC是一种GC,它可以自动释放内存,但与良好的垃圾收集器有许多区别。

You appear to be talking about Automatic Reference Counting, mentioned in other answers. ARC is a kind of GC in that it automates memory freeing, but has a number of differences from a good garbage collector.

首先,它主要是一种编译器技术。编译器知道Cocoa的引用计数指南,因此它根据规则插入保留和释放的位置。它的工作方式就像你自己编写保留和释放一样 - 它只是为你插入它们。正常的垃圾收集器会在程序运行时跟踪程序的内存。

Firstly, it's mainly a compiler technology. The compiler knows about Cocoa's reference-counting guidelines, so it inserts retains and releases where they should be according to the rules. It works just like if you'd written the retains and releases yourself — it simply inserts them for you. Normal garbage collectors keep track of your program's memory while it is running.

其次,因为它就像保留和释放一样,它无法捕获保留周期(如果是对象)保留对象B和对象B保留对象A,没有其他任何引用它们,它们都变成不朽的)。您需要采取相同的预防措施来防止它们。

Second, since it is just like retain and release, it can't catch retain cycles (if Object A retains Object B and Object B retains Object A, and nothing else references either of them, they both become immortal). You need to take the same precautions to prevent them.

它还使用与自动垃圾收集器不同的资源。与Objective-C一起使用的垃圾收集器必须扫描未引用的内存并收集它 - 这是昂贵的,并且可能导致在较慢的系统上口吃 - 但它们只需偶尔这样做,理论上甚至可以微调它们的收集周期与程序实际使用其内存的方式相匹配。通常,GC程序将使用比非GC程序更多的内存,并且当GC决定收集时将显着减慢。另一方面,ARC将扫描移动到编译时并在内存可用时立即释放内存,但它必须不断更新对象引用计数,而不是等待像收集器一样构建垃圾。

It also uses resources differently from an automatic garbage collector. The garbage collectors used with Objective-C have to scan for unreferenced memory and collect it — which is expensive, and can lead to "stuttering" on slower systems — but they only have to do this occasionally, and in theory can even fine-tune their collection cycles to match how a program actually uses its memory. In general, a GC program will use more memory than a non-GC program and will slow down significantly when the GC decides to collect. ARC, on the other hand, moves the "scanning" to compile-time and frees memory as soon as it's available, but it has to constantly update object reference counts instead of waiting for garbage to build up like a collector.

这篇关于iOS 5有垃圾收集吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆