auto release pool 应该用release还是drain

本文探讨了在iOS开发环境中drain与release方法的作用及其差异。在引用计数环境中,drain方法的行为与release相同,导致接收者被释放;在垃圾回收环境中,drain最终调用objc_collect_if_needed。在iOS开发中,两者作用相同。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文档里是这么说的:

drain

In a reference-counted environment, releases and pops the receiver; in a garbage-collected environment, triggers garbage collection if the memory allocated since the last collection is greater than the current threshold.

- (void)drain
Discussion

In a reference-counted environment, this method behaves the same as releaseSince an autorelease pool cannot be retained (see retain), this therefore causes the receiver to be deallocated. When an autorelease pool is deallocated, it sends a release message to all its autoreleased objects. If an object is added several times to the same pool, when the pool is deallocated it receives a release message for each time it was added.

In a garbage-collected environment, this method ultimately calls objc_collect_if_needed.

Special Considerations

In a garbage-collected environment, release is a no-op, so unless you do not want to give the collector a hint it is important to use drain in any code that may be compiled for a garbage-collected environment.

对于iOS来说drain和release的作用其实是一样的。

转载于:https://my.oschina.net/amoyai/blog/103502

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值