gpt4 book ai didi

ios - Objective-C,NSMutableData 未发布

转载 作者:行者123 更新时间:2023-11-29 12:27:51 24 4
gpt4 key购买 nike

我的应用有时会读取数据、使用数据,然后可能需要稍后再次读取。我注意到,如果我在数据发布后读取它,但如果我碰巧再次读取它,它就永远不会被发布。

为什么再次读取数据不释放?

代码:

#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

enter image description here

最佳答案

尝试将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com