iPhone Dev Tip: How to find out Available/Free Memory
27 November 2009
One Comment
The iPhone has limited memory,and as developer its our responsibility to manage memory optimally. On one hand caching improves performance while on other hand it uses ‘expensive’ memory, and we must optimally balance between both sides.
Once you received memory warning you can release unwanted objects to free up the memory, but knowing how much memory is available OR free on iPhone will help you make calculated decisions before low memory warning is even triggered. OR simply you can ask user to restart the iPhone and then launch the application.This Tip will help you calculate Available Memory for your application.
So, here is code to calculate available memory :
#import #import static void print_free_memory () { 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"); /* Stats in bytes */ 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; NSLog(@"used: %u free: %u total: %u", mem_used, mem_free, mem_total); }
Happy Coding :)
Related posts:
- iPhone Development Tip : Debugging Memory release I was struggling with a bug in my latest iPhone...
- iPhone Development Tip : How to Dynamically Initialize Class in Objective-C While working on an iPhone project i wanted a way...
- iPhone Development Tip : Custom UINavigationBar More and more developers are creating custom user elements these...









[...] This post was mentioned on Twitter by Gaurav Verma, Alltop. Alltop said: iPhone Dev Tip: How to find out Available/Free Memory http://bit.ly/8noR4x Tech.alltop [...]