The goal of the script is to generate logs that monitor a macOS machines RAM, CPU, Disk and Network Statistics.
I'm successfully able to display some memory stats using psutil.virtual_memory() or the vm_stat command in terminal.
psutil - https://psutil.readthedocs.io/en/latest/
However I would like to specifically display the 'cached files' stat shown in Activity Monitor (below).
I don't want to do this a ghetto way like clearing the cache and then measuring the available RAM before and after and subtracting.
Here's what what I'm playing with:
import psutil
mem = str(psutil.virtual_memory())
mem = mem.replace(',', '')
mem = mem.split()
mem_total = mem[0].split("=")
mem_total = mem_total[1]
mem_total = round(int(mem_total)/1024**3, ndigits=1)
mem_used = mem[3].split("=")
mem_used = mem_used[1]
mem_used = round(int(mem_used)/1024**3, ndigits=1)
mem_free = mem[4].split("=")
mem_free = mem_free[1]
mem_free = round(int(mem_free)/1024**3, ndigits=1)
print(mem_total)
print(mem_used)
print(mem_free)
Thanks

I programmed a tool exactly like this a little while ago called
Tocco, which I would encourage you to check out its code on Github. It also usespsutilto pull OSX system information. Here is the specific part of the code you are asking for, usingpsutil.virtual_memory():Outputs: