iOS - 设备内存获取

iOS - 设备内存获取_第1张图片
APPLE

方法一

#pragma mark - 手机可用内存

- (double)availableMemory
{
    vm_statistics_data_t vmStats;
    mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
    kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO,(host_info_t)&vmStats,&infoCount);
    
    if (kernReturn != KERN_SUCCESS) {
        return NSNotFound;
    }
    
    NSLog(@"%f",((vm_page_size *vmStats.free_count) / 1024.0) / 1024.0);
    
    return ((vm_page_size *vmStats.free_count) / 1024.0) / 1024.0;
}

方法二

NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *attributes = [fileManager attributesOfFileSystemForPath:NSHomeDirectory() error:nil];

NSLog(@"容量%.2fG",[attributes[NSFileSystemSize] doubleValue] / (powf(1024, 3)));
NSLog(@"可用%.2fG",[attributes[NSFileSystemFreeSize] doubleValue] / powf(1024, 3));

你可能感兴趣的:(iOS - 设备内存获取)