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?

有帮助吗?

解决方案

You cannot read the total RAM of your device. You can only read an estimate of your used vs free RAM. Your device will always use some memory (for system, for background apps, another processes) so you will never see your "should be 512" totally. Why the simulator give a close value? Man, it's the simulator... If you wanna know the RAM, read the tech specs of your device. But with the method posted by you, (as I said), you'll get the free/used mem. Cheers.

其他提示

There is an Git project which displays the max RAM available on your iPhone. It also allocates memory till it crashes. It saves the value for the memory warning as well as when the app crashed. On next start you get displayed the values on a bar.

Source

enter image description here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top