Posted By

espinallab on 05/04/11


Tagged


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

shalomfriss
Tintenklecks
cnathan


print_free_memory


 / Published in: Objective C
 

  1. #import <mach/mach.h>
  2. #import <mach/mach_host.h>
  3.  
  4.  
  5. -(void) print_free_memory {
  6. mach_port_t host_port;
  7. mach_msg_type_number_t host_size;
  8. vm_size_t pagesize;
  9.  
  10. host_port = mach_host_self();
  11. host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
  12. host_page_size(host_port, &pagesize);
  13.  
  14. vm_statistics_data_t vm_stat;
  15.  
  16. if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
  17. NSLog(@"Failed to fetch vm statistics");
  18.  
  19. /* Stats in bytes */
  20. natural_t mem_used = (vm_stat.active_count +
  21. vm_stat.inactive_count +
  22. vm_stat.wire_count) * pagesize;
  23. natural_t mem_free = vm_stat.free_count * pagesize;
  24. natural_t mem_total = mem_used + mem_free;
  25. int iUsed = round(mem_used/100000);
  26. int iFree = round(mem_free/100000);
  27. int iTotal = round(mem_total/100000);
  28. NSLog(@"used: %d free: %d total: %d", iUsed, iFree, iTotal);
  29. }
  30.  

Report this snippet  

You need to login to post a comment.