iOS 5有垃圾收集吗?

时间:2021-08-03 03:47:42

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?

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

3 个解决方案

#1


39  

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.

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

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.

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

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.

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

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.

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

#2


23  

On Apple's public iOS 5 page, they state:

在Apple的公共iOS 5页面上,他们声明:

Automatic Reference Counting

自动参考计数

Automatic Reference Counting (ARC) for Objective-C makes memory management the job of the compiler. By enabling ARC with the new Apple LLVM compiler, you will never need to type retain or release again, dramatically simplifying the development process, while reducing crashes and memory leaks. The compiler has a complete understanding of your objects, and releases each object the instant it is no longer used, so apps run as fast as ever, with predictable, smooth performance.

Objective-C的自动引用计数(ARC)使内存管理成为编译器的工作。通过使用新的Apple LLVM编译器启用ARC,您将永远不需要再次键入retain或release,从而大大简化了开发过程,同时减少了崩溃和内存泄漏。编译器完全了解您的对象,并在不再使用它的瞬间释放每个对象,因此应用程序运行速度与以往一样快,具有可预测的平滑性能。

It's a compiler feature; not an OS feature so I don't see why it wouldn't work with older versions.

这是一个编译器功能;不是操作系统功能,所以我不明白为什么它不适用于旧版本。

#3


9  

Automatic Reference Counting implements automatic memory management for Objective-C objects and blocks, freeing the programmer from the need explicitly insert retains and releases.

自动引用计数实现了Objective-C对象和块的自动内存管理,使程序员免于明确插入保留和释放的需要。

You will worry less about memory management. There is public info available on the subject:

您不必担心内存管理问题。有关于该主题的公共信息:

If the spec is too harsh to read, in short, you will need to edit the retain/release code to use ARC, but old programs remain compatible. What you shouldn't do is mix both.

如果规范太难以阅读,简而言之,您需要编辑保留/释放代码以使用ARC,但旧程序保持兼容。你不应该做的是混合两者。

#1


39  

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.

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

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.

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

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.

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

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.

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

#2


23  

On Apple's public iOS 5 page, they state:

在Apple的公共iOS 5页面上,他们声明:

Automatic Reference Counting

自动参考计数

Automatic Reference Counting (ARC) for Objective-C makes memory management the job of the compiler. By enabling ARC with the new Apple LLVM compiler, you will never need to type retain or release again, dramatically simplifying the development process, while reducing crashes and memory leaks. The compiler has a complete understanding of your objects, and releases each object the instant it is no longer used, so apps run as fast as ever, with predictable, smooth performance.

Objective-C的自动引用计数(ARC)使内存管理成为编译器的工作。通过使用新的Apple LLVM编译器启用ARC,您将永远不需要再次键入retain或release,从而大大简化了开发过程,同时减少了崩溃和内存泄漏。编译器完全了解您的对象,并在不再使用它的瞬间释放每个对象,因此应用程序运行速度与以往一样快,具有可预测的平滑性能。

It's a compiler feature; not an OS feature so I don't see why it wouldn't work with older versions.

这是一个编译器功能;不是操作系统功能,所以我不明白为什么它不适用于旧版本。

#3


9  

Automatic Reference Counting implements automatic memory management for Objective-C objects and blocks, freeing the programmer from the need explicitly insert retains and releases.

自动引用计数实现了Objective-C对象和块的自动内存管理,使程序员免于明确插入保留和释放的需要。

You will worry less about memory management. There is public info available on the subject:

您不必担心内存管理问题。有关于该主题的公共信息:

If the spec is too harsh to read, in short, you will need to edit the retain/release code to use ARC, but old programs remain compatible. What you shouldn't do is mix both.

如果规范太难以阅读,简而言之,您需要编辑保留/释放代码以使用ARC,但旧程序保持兼容。你不应该做的是混合两者。