标签:
#include <mach/mach.h>
//存储内存
- (float)getFreeDiskspace{
float totalSpace;
float totalFreeSpace=0.f;
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];
NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
totalSpace = [fileSystemSizeInBytes floatValue]/1024.0f/1024.0f/1024.0f;
totalFreeSpace = [freeFileSystemSizeInBytes floatValue]/1024.0f/1024.0f/1024.0f;
NSLog(@"Memory Capacity of %f GB with %f GB Free memory available.", totalSpace, totalFreeSpace);
} else {
NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %@", [error domain], [error code]);
}
return totalFreeSpace;
}
//可用运行内存
- (double)getAvailableBytes
{
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;
}
return (vm_page_size * vmStats.free_count) + (vmStats.inactive_count * vm_page_size);
}
- (double)getAvailableKiloBytes
{
return [self getAvailableBytes] / 1024.0;
}
- (double)getAvailableMegaBytes
{
return [self getAvailableKiloBytes] / 1024.0;
}
//-----------------------------------------------------
内存各种类型
原文网址http://www.cnblogs.com/bandy/archive/2012/08/15/2639742.html
调用memoryInfo()就能拿到内存信息了,它的类型是vm_statistics_data_t。这个结构体有很多字段,在logMemoryInfo()中展示了如何获取它们。注意这些字段大都是页面数,要乘以vm_page_size才能拿到字节数。
标签:
原文地址:http://blog.csdn.net/u014202635/article/details/46546121