Skip to content

Instantly share code, notes, and snippets.

@mauiaaron
Last active December 23, 2015 07:09
Show Gist options
  • Save mauiaaron/6599103 to your computer and use it in GitHub Desktop.
Save mauiaaron/6599103 to your computer and use it in GitHub Desktop.
Checking available memory on iOS.
#include <stdio.h>
#include <mach/mach.h>
natural_t mach_getTotalBytes() {
static natural_t totalBytes=0;
if (totalBytes>0) {
return totalBytes;
}
mach_port_t host_port;
mach_msg_type_number_t host_vm_size;
vm_size_t pagesize;
host_port = mach_host_self();
host_vm_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
host_page_size(host_port, &pagesize);
vm_statistics_data_t vm_stat = { 0 };
kern_return_t kerr = host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_vm_size);
if (kerr != KERN_SUCCESS) {
return 0;
}
natural_t freeBytes = vm_stat.free_count * pagesize;
natural_t wiredBytes = vm_stat.wire_count * pagesize;
natural_t activeBytes = vm_stat.active_count * pagesize;
natural_t inactiveBytes = vm_stat.inactive_count * pagesize;
totalBytes = wiredBytes + freeBytes + activeBytes + inactiveBytes;
printf("statistics free: %u wired: %u active: %u inactive: %u total: %u\n", freeBytes, wiredBytes, activeBytes, inactiveBytes, totalBytes);
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr2 = task_info(mach_task_self(),
TASK_BASIC_INFO,
(task_info_t)&info,
&size);
if (kerr2 == KERN_SUCCESS) {
printf("statistics Memory in use (in bytes): %u virtual_size: %u\n", info.resident_size, info.virtual_size);
}
return totalBytes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment