144 lines
4.2 KiB
Plaintext
144 lines
4.2 KiB
Plaintext
#include <mach/mach.h>
|
|
#include <mach/host_info.h>
|
|
#include <mach/mach_init.h>
|
|
#include <mach/mach_host.h>
|
|
#include <mach/task.h>
|
|
#include <mach/task_info.h>
|
|
#include <sys/types.h>
|
|
#include <sys/sysctl.h>
|
|
|
|
//get memry useage
|
|
double getUsedMemoryMByte()
|
|
{
|
|
task_basic_info_data_t info;
|
|
mach_msg_type_number_t info_count = TASK_BASIC_INFO_COUNT;
|
|
|
|
kern_return_t k_ret = task_info( mach_task_self(),
|
|
TASK_BASIC_INFO,
|
|
(task_info_t)&info,
|
|
&info_count);
|
|
|
|
if (k_ret != KERN_SUCCESS)
|
|
return 0.0f;
|
|
else
|
|
return info.resident_size / 1024.0 / 1024.0;;
|
|
}
|
|
|
|
|
|
BOOL memoryInfo(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);
|
|
|
|
return kernReturn == KERN_SUCCESS;
|
|
}
|
|
|
|
void logMemoryInfo() {
|
|
vm_statistics_data_t vmStats;
|
|
|
|
if (memoryInfo(&vmStats)) {
|
|
NSLog(@"free: %u\nactive: %u\ninactive: %u\nwire: %u\nzero fill: %u\nreactivations: %u\npageins: %u\npageouts: %u\nfaults: %u\ncow_faults: %u\nlookups: %u\nhits: %u",
|
|
vmStats.free_count * vm_page_size,
|
|
vmStats.active_count * vm_page_size,
|
|
vmStats.inactive_count * vm_page_size,
|
|
vmStats.wire_count * vm_page_size,
|
|
vmStats.zero_fill_count * vm_page_size,
|
|
vmStats.reactivations * vm_page_size,
|
|
vmStats.pageins * vm_page_size,
|
|
vmStats.pageouts * vm_page_size,
|
|
vmStats.faults,
|
|
vmStats.cow_faults,
|
|
vmStats.lookups,
|
|
vmStats.hits
|
|
);
|
|
}
|
|
}
|
|
|
|
double getFreeMemoryMByte()
|
|
{
|
|
vm_statistics_data_t vmStats;
|
|
if (memoryInfo(&vmStats))
|
|
{
|
|
return vmStats.free_count * vm_page_size / 1024.0 / 1024.0;
|
|
}
|
|
return 0.0;
|
|
}
|
|
|
|
uint64_t getFreeDiskspace ()
|
|
{
|
|
uint64_t totalSpace = 0;
|
|
uint64_t totalFreeSpace = 0;
|
|
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 unsignedLongLongValue];
|
|
totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
|
|
NSLog(@"Memory Capacity of %llu MiB with %llu MiB Free memory available.", ((totalSpace/1024ll)/1024ll), ((totalFreeSpace/1024ll)/1024ll));
|
|
}
|
|
else
|
|
{
|
|
NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld", [error domain], [error code]);
|
|
}
|
|
|
|
return totalFreeSpace;
|
|
}
|
|
|
|
double get_RAMsizeMByte() // 手机最大RAM
|
|
{
|
|
size_t size = sizeof(int);
|
|
int results;
|
|
int mib[2] = {CTL_HW, HW_PHYSMEM};
|
|
sysctl(mib, 2, &results, &size, NULL, 0);
|
|
/*
|
|
mach_port_t host_port;
|
|
mach_msg_type_number_t host_size;
|
|
vm_size_t pagesize;
|
|
|
|
host_port = mach_host_self();
|
|
host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
|
|
host_page_size(host_port, &pagesize);
|
|
|
|
vm_statistics_data_t vm_stat;
|
|
|
|
if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
|
|
NSLog(@"Failed to fetch vm statistics");
|
|
|
|
|
|
natural_t mem_used = (vm_stat.active_count +
|
|
vm_stat.inactive_count +
|
|
vm_stat.wire_count) * pagesize;
|
|
natural_t mem_free = vm_stat.free_count * pagesize;
|
|
natural_t mem_total = mem_used + mem_free;
|
|
*/
|
|
double ret = results /1024.0/1024.0;
|
|
return ret;
|
|
}
|
|
|
|
extern "C"
|
|
{
|
|
double MemoryStat_GetFreeDiskspaceMByte()
|
|
{
|
|
double mbyte = getFreeDiskspace() / 1024.0 /1024.0;
|
|
return mbyte;
|
|
}
|
|
|
|
double MemoryStat_GetFreeMemoryMByte()
|
|
{
|
|
return getFreeMemoryMByte();
|
|
}
|
|
|
|
double MemoryStat_GetAppUsedMemoryMByte()
|
|
{
|
|
return getUsedMemoryMByte();
|
|
}
|
|
|
|
double MemoryStat_GetRAMByte()
|
|
{
|
|
return get_RAMsizeMByte();
|
|
}
|
|
}
|