Mac*_*uch 2 iphone crash memory-leaks
我再一次在我的代码中寻找内存泄漏和其他疯狂错误.:)
我有一个常用文件的缓存(图像,数据记录等,TTL大约一周,大小限制缓存(100MB)).目录中有时超过15000个文件.在应用程序退出时,缓存会写入一个控制文件,其中包含当前缓存大小以及其他有用信息.如果应用程序由于某种原因崩溃(有时会发生),我在这种情况下计算应用程序启动时所有文件的大小,以确保我知道缓存大小.我的应用程序崩溃,因为内存不足,我不知道为什么.
内存泄漏检测器根本不显示任何泄漏.我也没有看到.下面的代码有什么问题?有没有其他快速的方法来计算iPhone上目录中所有文件的总大小?也许没有枚举目录的全部内容?代码在主线程上执行.
NSUInteger result = 0;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDirectoryEnumerator *dirEnum = [[[NSFileManager defaultManager] enumeratorAtPath:path] retain];
int i = 0;
while ([dirEnum nextObject]) {
NSDictionary *attributes = [dirEnum fileAttributes];
NSNumber* fileSize = [attributes objectForKey:NSFileSize];
result += [fileSize unsignedIntValue];
if (++i % 500 == 0) { // I tried lower values too
[pool drain];
}
}
[dirEnum release];
dirEnum = nil;
[pool release];
pool = nil;
Run Code Online (Sandbox Code Playgroud)
谢谢,MacTouch
排出池"释放"它,它不只是清空它.将自动释放池视为堆栈,因此您已经弹出了自己的,这意味着所有这些新对象都将进入主自动释放池,并且在弹出之前不会被清除.相反,将自动释放池的创建移动到循环内部.你可以做点什么
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
int i = 0;
while( shouldloop ) {
// do stuff
if( ++i%500 == 0 ) {
[pool drain];
pool = [[NSAutoreleasePool alloc] init];
}
}
[pool drain];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1481 次 |
| 最近记录: |