我的应用有时会读取数据、使用数据,然后可能需要稍后再次读取。我注意到,如果我在数据发布后读取它,但如果我碰巧再次读取它,它就永远不会被发布。
为什么再次读取数据不释放?
代码:
#import "AppDelegate.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self readData]; // read one
NSLog(@"read1");
sleep(1);
[self readData2];
}
- (void)readData2 {
[self readData]; // read two
NSLog(@"read2");
}
- (void)readData {
__block NSMutableData *data = [NSMutableData dataWithContentsOfFile:@"test"]; // file is 125 MB
NSUInteger size = [data length];
for (NSUInteger i = 0; i < size; i++) {
// do stuff
}
return;
}
@end
最佳答案
尝试将 dataWithContentsOfFile:options:error
与选项一起使用:
NSDataReadingMappedIfSafe
A hint indicating the file should be mapped into virtual memory, if possible and safe.
虽然 dataWithContentsOfFile
的描述没有说明,但不使用 options
可能会导致数据被保留,否则。
dataWithContentsOfFile
This method is equivalent to dataWithContentsOfFile:options:error: with no options. If you need to know what was the reason for failure, use dataWithContentsOfFile:options:error:.
关于ios - Objective-C,NSMutableData 未发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28553067/