Mac OS X sysctl get total and free memory size

How to get on MAC OS X using sysctl used and free memory ? Searched for sysctl -a | grep vm or sysctl -a | grep mem but didn't found anything relevant.


Solution 1:

You don't.

Well, except for total memory size, which you could have found with sysctl -a | grep mem (hw.memsize = 4294967296 on my machine).

vm_stat displays the same information as Activity Monitor.app does, you just need to multiply the value you want by page size. Both are provided in the output.

Solution 2:

No grep is needed.

sysctl -n hw.memsize

Solution 3:

top -l 1 | grep PhysMem: | awk '{print $10}'

top -l 1 runs top in logging mode (e.g. file output, not display/terminal output) for one iteration, then quits. The grep/awk filters for the free memory value in top output.