我从我的主包中加载了一个静态属性列表。很简单。但是,在我将其设置为 nil 后,属性列表不会被释放。为什么是这样?分配的内存很容易监控,因为属性列表很大。
NSPropertyListFormat format = NSPropertyListBinaryFormat_v1_0;
NSError *error = nil;
NSArray *myArray = [NSPropertyListSerialization propertyListWithData:[NSData dataWithContentsOfFile:bundleFilePath]
options:NSPropertyListImmutable
format:&format
error:&error];
myArray = nil;
// Still takes up allocated space (as seen in memory monitor via instruments).
使用自动释放池:
- (void)someMethodCalledOnTheMainThread
{
@autoreleasePool {
NSString *bundleFilePath = @"...";
NSPropertyListFormat format = NSPropertyListBinaryFormat_v1_0;
NSError *error = nil;
NSArray *myArray = [NSPropertyListSerialization propertyListWithData:[NSData dataWithContentsOfFile:bundleFilePath]
options:NSPropertyListImmutable
format:&format
error:&error];
}
}