我从以下方法获取可用内存:
static void print_free_memory () {
float totalSpace = 0.0f;
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
if (dictionary) {
NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];
totalSpace = [fileSystemSizeInBytes floatValue];
NSLog(@"Total memory:%f",totalSpace);
freeMemAvailable = [[dictionary objectForKey:NSFileSystemFreeSize] floatValue];
NSLog(@"Free Memory:%f",freeMemAvailable);
} else {
NSLog(@"Error Obtaining File System Info: Domain = %@, Code = %@", [error domain], [error code]);
}
}
上述方法返回 = 9139.650391 mb ~ 9.14 GB 并且设备上显示的设备内存 = 8.4 GB
大约有 700 MB 的差异。为什么会这样?有什么建议么。
我在device上查看设备内存的路径如下。转到:-设置->常规->关于->可用