获取手机系统大小、可用空间大小,设备可用内存及当前应用所占内存等

设备可用内存及当前应用所占内存

  1. // 获取当前设备可用内存及所占内存的头文件    
  2. #import <sys/sysctl.h>    
  3. #import <mach/mach.h>    
  4.      
  5. // 获取当前设备可用内存(单位:MB)    
  6. - (double)availableMemory    
  7. {    
  8.   vm_statistics_data_t vmStats;    
  9.   mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;    
  10.   kern_return_t kernReturn = host_statistics(mach_host_self(),     
  11.                                              HOST_VM_INFO,     
  12.                                              (host_info_t)&vmStats,     
  13.                                              &infoCount);    
  14.       
  15.   if (kernReturn != KERN_SUCCESS) {    
  16.     return NSNotFound;    
  17.   }    
  18.       
  19.   return ((vm_page_size *vmStats.free_count) / 1024.0) / 1024.0;    
  20. }    
  21.     
  22. // 获取当前任务所占用的内存(单位:MB)    
  23. - (double)usedMemory    
  24. {    
  25.   task_basic_info_data_t taskInfo;    
  26.   mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT;    
  27.   kern_return_t kernReturn = task_info(mach_task_self(),     
  28.                                        TASK_BASIC_INFO,     
  29.                                        (task_info_t)&taskInfo,     
  30.                                        &infoCount);    
  31.     
  32.   if (kernReturn != KERN_SUCCESS    
  33.       ) {    
  34.     return NSNotFound;    
  35.   }    
  36.       
  37.   return taskInfo.resident_size / 1024.0 / 1024.0;    
  38. }    

手机系统大小、可用空间大小
  1. +(uint64_t)getFreeDiskspace {  
  2.     uint64_t totalSpace = 0.0f;  
  3.     uint64_t totalFreeSpace = 0.0f;  
  4.     NSError *error = nil;  
  5.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  6.     NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];  
  7.       
  8.     if (dictionary) {  
  9.         NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];  
  10.         NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];  
  11.         totalSpace = [fileSystemSizeInBytes floatValue];  
  12.         totalFreeSpace = [freeFileSystemSizeInBytes floatValue];  
  13.         NSLog(@"Memory Capacity of %llu GB with %llu GB Free memory available.", ((totalSpace/1024ll)/1024ll/1024ll), ((totalFreeSpace/1024ll)/1024ll/1024ll));  
  14.     } else {  
  15.         NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %d", [error domain], [error code]);  
  16.     }  
  17.       
  18.     return totalFreeSpace;  
  19. }  

其中
  1. attributesOfFileSystemForPath:error:  
返回的是一个字典。感兴趣的同学可用自己看下里面的其他信息
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值