By "accurately describes the amount of used memory", I mean does it report (or can it be calculated) the same amount of memory use as described in Activity Monitor?
My intent is to find a method to programmatically access the amount of used physical memory in the system. In .NET Core 3.1 on Mac, this is impossible. My next option is to query a command line tool. My concern is that the figures reported below do not total up to 14.20 GB of used memory as expected in the above screenshot. This is why I claim they are inaccurate.
Below is what I've tried so far and why this is not a duplicate question. All commands were ran a second apart from each other after the above screenshot was taken.
- Is there a Mac OS X Terminal version of the "free" command in Linux systems? The accepted answer's Python script does not provide accurate numbers. "Real Mem Total" (which is calculated by summing the rss of all running processes and converting it to bytes) comes up to 11.2 GB when in reality I'm using 14.20 GB.
Wired Memory: 5247 MB Active Memory: 4170 MB Inactive Memory: 3813 MB Free Memory: 609 MB Real Mem Total (ps): 11226.922 MB
top's "PhysMem" field claims that I'm using 15 GB on my system is in use. This is somewhat close, but it is has very rough rounding and sometimes rounds up to 16 GB in use.
PhysMem: 15G used (5269M wired), 960M unused.
- How to calculate used memory on Mac OS by command line? The only answer describes
vm_statwhich is inaccurate and does not properly answer the question. I've read from around the web that to get the amount of used memory, you must sum "Active", "Inactive", and "Wired down" rows, then multiply it by page size. On this Catalina machine, this comes up to 13.8 GB (output below). Still kind of close, but this math is off by more than a GB on another Catalina machine.
Mach Virtual Memory Statistics: (page size of 4096 bytes) Pages free: 161443. Pages active: 1065326. Pages inactive: 974107. Pages speculative: 89929. Pages throttled: 0. Pages wired down: 1343201. Pages purgeable: 18630. "Translation faults": 2149730807. Pages copy-on-write: 123210957. Pages zero filled: 965651547. Pages reactivated: 129523087. Pages purged: 3285194. File-backed pages: 292639. Anonymous pages: 1836723. Pages stored in compressor: 3394212. Pages occupied by compressor: 559818. Decompressions: 166263782. Compressions: 206698757. Pageins: 358184714. Pageouts: 10064704. Swapins: 123517427. Swapouts: 126184348.
memory_pressuredoesn't seem to provide any accurate information related to memory usage. "System-wide memory free percentage" claims to be 54%, but 14.2 / 16 GB are in use.
The system has 2147483648 (524288 pages with a page size of 4096). Stats: Pages free: 162828 Pages purgeable: 16659 Pages purged: 3285194 Swap I/O: Swapins: 123517427 Swapouts: 126184348 Page Q counts: Pages active: 1062737 Pages inactive: 972247 Pages speculative: 89910 Pages throttled: 0 Pages wired down: 1346397 Compressor Stats: Pages used by compressor: 559818 Pages decompressed: 166263778 Pages compressed: 206698757 File I/O: Pageins: 358184697 Pageouts: 10064704 System-wide memory free percentage: 54%