在ARC下,是否仍建议为循环创建@autoreleasepool? [英] Under ARC, is it still advisable to create an @autoreleasepool for loops?

查看:64
本文介绍了在ARC下,是否仍建议为循环创建@autoreleasepool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个循环,该循环返回一堆自动释放的NSData对象...

Let's say that I have a loop that returns a bunch of autoreleased NSData objects...

NSData* bigData = ...
while(some condition) {
    NSData* smallData = [bigData subdataWithRange:...];
    //process smallData
}

在ARC下,我是否还应该在while条件周围包裹一个@autoreleasepool?

Under ARC, should I still wrap an @autoreleasepool around the while condition?

NSData* bigData = ...
@autoreleasepool {
    while(some condition) {
        NSData* smallData = [bigData subdataWithRange:...];
        //process smallData
    }
}

我问的原因是我看到了我的NSData对象通过屋顶的仪器中的动态分配计数,这些对象调用dataWith...方法而不是initWith...方法.当我使用initWith...时,生活分配的数量要少得多.

The reason why I'm asking is I see the living allocation count in instruments going through the roof for my NSData objects that invoke a dataWith... method as opposed to an initWith... method. When I use initWith..., the living allocation count is much, much less.

是否最好尽可能使用initWith...方法?

Is it better to prefer the initWith... methods whenever possible?

推荐答案

是的,在紧密循环中使用便捷方法时,仍应使用自动释放池.所有旧的内存管理规则仍然适用于ARC,编译器只是为您注入RR.查看真棒Mike Ash的精彩文章!

Yes you should still use autorelease pools when using convenience methods in a tight loop. All the old memory management rules still apply under ARC, the compiler is merely injecting RRs for you. Checkout the great post by the awesome Mike Ash!

链接

这篇关于在ARC下,是否仍建议为循环创建@autoreleasepool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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