Take the 2-minute tour ×

Why does host_statistics64() in OS X 10.6.8 (I don't know if other versions have this problem) return counts for free, active, inactive, and wired memory that don't add up to the total amount of ram? And why is it missing an inconsistent number of pages?

The following output represents the number of pages not classified as free, active, inactive, or wired over ten seconds (sampled roughly once per second).


The code that produces the numbers above is:

#include <stdio.h>
#include <mach/mach.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <unistd.h>

int main(int argc, char** argv) {
        struct vm_statistics64 stats;
        mach_port_t host = mach_host_self();
        natural_t count = HOST_VM_INFO64_COUNT;
        natural_t missing = 0;
        kern_return_t ret;
        int mib[2];
        long ram;
        natural_t pages;
        size_t length;
        int i;

        mib[0] = CTL_HW;
        mib[1] = HW_MEMSIZE;
        length = sizeof(long);
        sysctl(mib, 2, &ram, &length, NULL, 0);
        pages = ram / getpagesize();

        for (i = 0; i < 10 i++) {
                if ((ret = host_statistics64(host, HOST_VM_INFO64, (host_info64_
t)&stats, &count)) != KERN_SUCCESS) {

                missing = pages - (stats.free_count + stats.active_count + stats
.inactive_count + stats.wire_count);

                printf("%i\n", missing);

        return 0;
share|improve this question
vm_stat, which uses the same code, has the same not-quite-adding-up issue. So, it's not a problem in your code per se. –  nneonneo Feb 9 '13 at 16:12
With ML and 8 GB RAM, the missing part is bigger (around 2400). –  patrix Feb 9 '13 at 16:27
And have a look at opensource.apple.com/source/top/top-73/libtop.c, there seems to be some special calculation going on in there. –  patrix Feb 9 '13 at 16:37
@nneonneo Oh, I know. I got here by trying to figure out why vm_stat was giving me bad data. –  Chas. Owens Feb 9 '13 at 16:55
@patrix I don't know if this is a coincidence or not, but that seems about right. My worst case is between 500 and 600 pages missing and I have two CPUs. If we scale that up to 8 CPUs you would see 2000 to 2400 pages missing. If this is the case, then those pages are not currently claimed by any CPU; which means it is likely they are in the process of being transfered between CPUs. –  Chas. Owens Feb 10 '13 at 2:24
show 2 more comments


Your Answer



Browse other questions tagged or ask your own question.