MacOS – How to calculate used memory on Mac OS by command line

macmacosmemory

Below is a screenshot of Activity Monitor on mac os. I wonder what command line I can use to get the memory usage. I tried below command:

$ top -l 1 -n 0
Processes: 399 total, 3 running, 396 sleeping, 2460 threads 
2018/03/07 19:54:01
Load Avg: 7.41, 6.55, 6.23 
CPU usage: 58.38% user, 12.97% sys, 28.63% idle 
SharedLibs: 191M resident, 54M data, 24M linkedit.
MemRegions: 193162 total, 6016M resident, 107M private, 3067M shared.
PhysMem: 16G used (3042M wired), 70M unused.
VM: 2858G vsize, 1100M framework vsize, 178320755(0) swapins, 184739800(0) swapouts.
Networks: packets: 10995802/17G in, 9674758/13G out.
Disks: 15617288/823G read, 11170324/846G written.

These two lines:

MemRegions: 193162 total, 6016M resident, 107M private, 3067M shared.
PhysMem: 16G used (3042M wired), 70M unused.

tells me the Physical Memory(16G + 70M), Wired Memory(3042M) and Compressed memory(107M + 3067M). But I don't know how to calculate the App Memory shown in the screenshot. Does anyone know how to get the App memory value? I need that one to calculate the total used memory on Mac OS.

enter image description here

Best Answer

You can use the macOS equivalent to unix vmstat, which is vm_stat.

➜ vm_stat
Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free:                              139824.
Pages active:                           1673261.
Pages inactive:                         1073096.
Pages speculative:                         2909.
Pages throttled:                              0.
Pages wired down:                        788478.
Pages purgeable:                         226206.
[...]

To calculate the memory, multiply the number of pages with the page size (4096 bytes) and divide by 1024 to adjust the prefix to your needs.