tags:

views:

429

answers:

3

We're trying to find out how much physical memory is installed in a machine running Mac OS X. We've found the BSD function sysctl(). The problem is this function wants to return a 32 bit value but some Macs are able to address up to 32 GB which will not fit in a 32 bit value. (Actually even 4 GB won't fit in a 32 bit value.) Is there another API available on OS X (10.4 or later) that will give us this info?

+1  A: 

Did you try googling?

This seems to be the answer: http://lists.apple.com/archives/scitech/2005/Aug/msg00004.html

sysctl() does work, you just need to fetch hw.memsize instead of hw.physmem. hw.memsize will give you a uint64_t, so no 32 bit problem.

sleske
A: 

The answer is to use sysctl to get hw.memsize as was suggested in a previous answer. Here's the actual code for doing that.

#include <sys/types.h>
#include <sys/sysctl.h>

...

    int mib[2];
    int64_t physical_memory;
    size_t length;

    // Get the Physical memory size
    mib[0] = CTL_HW;
    mib[1] = HW_MEMSIZE;
    length = sizeof(int64);
    sysctl(mib, 2, &physical_memory, &length, NULL, 0);
Mike Taylor