我上具有使用Web服务和核心数据在一个紧密的循环同步过程中的iPad应用工作。根据Apple's Recomendation我分配减少内存占用和周期性漏极的NSAutoreleasePool
。这目前效果很好,目前的应用程序没有内存问题。但是,我打算转移到ARC,因为NSAutoreleasePool
不再有效,并希望保持这种相同的性能。我创建了几个例子和定时他们我想知道什么是最好的方法,使用ARC,以acheive同类性能和维护代码的可读性。降低峰值内存使用@autoreleasepool
出于测试目的,我想出了3个场景,每个创建使用1和10,000,000之间的数字的字符串。我跑了每个示例3次,以确定他们是否使用与苹果LLVM编译器3.0在Mac 64位应用程序所用的时间(W/O GDB -O0)和XCode的4.2。我还通过仪器运行每个示例来大致了解记忆峰值。
以下每个都包含在下面的代码块中的例子:
int main (int argc, const char * argv[])
{
@autoreleasepool {
NSDate *now = [NSDate date];
//Code Example ...
NSTimeInterval interval = [now timeIntervalSinceNow];
printf("Duration: %f\n", interval);
}
}
NSAutoreleasePool批次[原件预ARC](峰值记忆:〜116 KB)
static const NSUInteger BATCH_SIZE = 1500;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for(uint32_t count = 0; count < MAX_ALLOCATIONS; count++)
{
NSString *text = [NSString stringWithFormat:@"%u", count + 1U];
[text class];
if((count + 1) % BATCH_SIZE == 0)
{
[pool drain];
pool = [[NSAutoreleasePool alloc] init];
}
}
[pool drain];
运行时间:
10.928158
10.912849
11.084716
外@autoreleasepool(峰值存储器:〜382 MB)
@autoreleasepool {
for(uint32_t count = 0; count < MAX_ALLOCATIONS; count++)
{
NSString *text = [NSString stringWithFormat:@"%u", count + 1U];
[text class];
}
}
运行时间:
11.489350
11.310462
11.344662
内@autoreleasepool(峰值记忆:〜61.2KB)
for(uint32_t count = 0; count < MAX_ALLOCATIONS; count++)
{
@autoreleasepool {
NSString *text = [NSString stringWithFormat:@"%u", count + 1U];
[text class];
}
}
运行时间:
14.031112
14.284014
14。099625
@autoreleasepool瓦特/ GOTO(峰值存储器:〜115KB)
static const NSUInteger BATCH_SIZE = 1500;
uint32_t count = 0;
next_batch:
@autoreleasepool {
for(;count < MAX_ALLOCATIONS; count++)
{
NSString *text = [NSString stringWithFormat:@"%u", count + 1U];
[text class];
if((count + 1) % BATCH_SIZE == 0)
{
count++; //Increment count manually
goto next_batch;
}
}
}
运行时间:
10.908756
10.960189
11.018382
的goto
声明提供最接近的性能,但它采用的是goto
。有什么想法吗?
更新:
注:goto
声明是在documentation表示不会泄漏内存的@autoreleasepool一个正常的退出。
在进入时,会推送自动释放池。在正常退出(中断, 返回,转到,通过,等等)自动释放池被弹出。 为了与现有代码兼容,如果退出是由于例外造成的,则 自动释放池不会弹出。
使用优化。这对ARC代码来说非常重要。 – 2012-03-12 21:58:20
因此'goto'肯定不是,我不知道,导致内存泄漏?其他一切都合情合理:减少排水速度更快。无论如何,我只能评论可读性:在任何你游泳池都很好。那个goto需要一个黄色的粘滞便笺。 – 2012-03-12 21:58:42
goto似乎没有泄漏任何内存。看起来像我期望的那样排除了autorelease池的范围,但我不是ARC的专家(还),并且不想依赖UB。 – Joe 2012-03-12 22:07:06