Are there any methods in (C)Python to inspect the process' current memory usage? In particular, I'd like to determine the high-water mark of memory usage in a testing script, but if necessary I don't mind periodically checking memory usage and calculating the high water mark for myself.
EDIT: I'm looking for either a pure-python solution, or something which works on OS X.
Have a look at my answer to this question. It uses the getrusage()
function from the standard library's resource
module, and works on Mac OS X.
On Linux, you can inspect the /proc/self/status
file:
VmPeak: 6784 kB
VmSize: 6784 kB
VmLck: 0 kB
VmHWM: 572 kB
VmRSS: 572 kB
VmData: 180 kB
VmStk: 136 kB
VmExe: 44 kB
VmLib: 1640 kB
VmPTE: 36 kB
VmSwap: 0 kB
Probably VmPeak
is the line you are most interested in, but if you mmap(2)
a gigabyte-sized file, you'll probably be accounted for over a gigabyte, even if you only use three or four pages from the file.
If you're aware of the limitations of checking memory via top(1)
or ps(1)
then you're probably good to go. If you're not aware of the limitations of checking memory use, then be sure to look into the meanings of the Virt
, Res
, and Shr
columns in top(1)
output. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With