I want to know the Total RAM available in my iPhone. For this I've used the following code.
Note: Please do not interpret the question as to retrieve RAM statistics such as Wired, Inactive, Active and Free.
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");
return;
}
/* Stats in bytes */
self.wired = vm_stat.wire_count * pagesize / (1024 * 1024);
self.active = vm_stat.active_count * pagesize / (1024 * 1024);
self.inactive = vm_stat.inactive_count * pagesize / (1024 * 1024);
self.free = vm_stat.free_count * pagesize / (1024 * 1024);
self.used = self.active + self.inactive + self.wired;
self.total = self.used + self.free;
Here are the results:
- Simulator: total memory = 2045 (my PC contains 2GB RAM). Seems correct.
- Device (iPhone 4): total memory = 390 (should be 512). Incorrect.
- Device (iPhone 3GS): total memory = 84 (should be 256). Incorrect.
Please let me know if this is the correct way to calculate the TOTAL RAM of an iDevice?