Hi,
I am using the below code to compute the free memory in iOS.
uint64_t memAvailable() { mach_port_t host_port = mach_host_self(); if(host_port == MACH_PORT_NULL) { return 0; } vm_size_t pagesize; host_page_size(host_port, &pagesize);
vm_statistics_data_t vm_stat;
mach_msg_type_number_t host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
{
tdlog(LOG_LEVEL_DEBUG, @"Failed to fetch vm statistics");
return 0;
}
/* Stats in bytes */
return vm_stat.free_count * pagesize;
}
My qs is do we need to call mach_port_deallocate() to release the reference count on host_port??
Please advise.
do we need to call
mach_port_deallocate
to release the reference count onhost_port
?
Yes and no. In theory you should release every reference you get but the host port lives forever and thus you don’t actually need to do that in this case.
Having said that, the code you posted suggests you’re trying to calculate the device’s ‘free memory’, which is generally a nonsense value. I discuss that extensively in On Free Memory. And that reminded me that I should update the post to cover os_proc_available_memory
, which is most likely the droid you’re looking for here.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"