How to find memory usage in mac OSX?
Asked Answered
W

1

7

How to find memory usage like an activity monitor display in osx 10.9 and above. i have used following code to fetch memory usage.

but there is some difference between what it shows in activity monitor and what is i find by this code.

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_statistics64_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");
}
float free_count = vm_stat.free_count * pagesize;

float active_count=vm_stat.active_count *pagesize;

float inactive_count=vm_stat.inactive_count * pagesize;

float wire_used=vm_stat.wire_count *pagesize;

float zero_fill_count=vm_stat.zero_fill_count * pagesize;

float reactivations=vm_stat.reactivations *pagesize;

float pageins=vm_stat.pageins *pagesize;

float pageouts=vm_stat.pageouts *pagesize;

float faults=vm_stat.faults *pagesize;

float cow_faults=vm_stat.cow_faults * pagesize;

float lookups=vm_stat.lookups *pagesize;

float hits=vm_stat.hits * pagesize;

float purgeable_count=vm_stat.purgeable_count * pagesize;

float purges=vm_stat.purges *pagesize;

float speculative_count=vm_stat.speculative_count *pagesize;

I am also wondering that which should i consider as App memory, File caches, wired memory and compressed memory.

Here, wired count i got using this code is same as displaying in an activity monitor.

Can some one help me out here. Thanks

Wahoo answered 13/10, 2014 at 9:34 Comment(1)
+1 Tks. Awesome question :).Auliffe
L
1

You can find App memory using this appMemory = vm_page_size * (vm_stat.internal_page_count - vm_stat.purgeable_count);

Lysol answered 27/7, 2015 at 13:6 Comment(1)
How to find vm_stat.internal_page_count ?Klement

© 2022 - 2024 — McMap. All rights reserved.