In a garbage-collected environment, there is no need for autorelease pools. You may, however, write a framework that is designed to work in both a garbage-collected and reference-counted environment. In this case, you can use autorelease pools to hint to the collector that collection may be appropriate. In a garbage-collected environment, sending a drain message to a pool triggers garbage collection if necessary; release, however, is a no-op. In a reference-counted environment, drain has the same effect as release. Typically, therefore, you should use drain instead of release.
在引用计数方式下,release与drain的效果是一样的。
在垃圾回收环境中,release
是一个空操作。因此,NSAutoreleasePool
提供了drain
方法,在引用计数环境中,该方法的作用等同于调用release
,但在垃圾回收环境中,它会触发垃圾回收(如果自上次垃圾回收以来分配的内存大于当前的阈值)。因此,在通常情况下,您应该使用drain
而不是release
来销毁自动释放池。