Retrieve RAM info on a Mac?

后端 未结 4 2020
悲&欢浪女
悲&欢浪女 2021-02-04 16:53

I need to retrieve the total amount of RAM present in a system and the total RAM currently being used, so I can calculate a percentage. This is similar to: Retrieve system infor

4条回答
  •  耶瑟儿~
    2021-02-04 17:26

    You're not supposed to read from /usr/bin/vm_stat, rather you're supposed to run it; it is a program. Look at the first four lines of output

    Pages free:                  1880145.
    Pages active:                  49962.
    Pages inactive:                43609.
    Pages wired down:             123353.
    

    Add the numbers in the right column and multiple by the system page size (as returned by getpagesize()) and you get the total amount of physical memory in the system in bytes.

    vm_stat isn't setuid on Mac OS, so I assume there is a non-privileged API somewhere to access this information and that vm_stat is using it. But I don't know what that interface is.

提交回复
热议问题