Determine physical mem size programmatically on OSX

前端 未结 4 1180
天命终不由人
天命终不由人 2020-12-29 14:33

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

相关标签:
4条回答
  • 2020-12-29 14:59

    From Obtaining a Mac’s System Profiler data from shell:

    Use system_profiler.

    0 讨论(0)
  • 2020-12-29 15:13

    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.

    0 讨论(0)
  • 2020-12-29 15:13

    Alternatively you can add the data from vm_statistics_data_t to get the total memory

    vm_statistics_data_t vm_stat;
    int count = HOST_VM_INFO_COUNT;
    kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (integer_t*)&vm_stat, (mach_msg_type_number_t*)&count);
    
    0 讨论(0)
  • 2020-12-29 15:16

    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_t);
        sysctl(mib, 2, &physical_memory, &length, NULL, 0);
    
    0 讨论(0)
提交回复
热议问题