Is it possible to get a "high water mark" of memory usage from Python?
Are there any methods in (C)Python to inspect the p开发者_C百科rocess' 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. :)
you can use os.getpid()
to get your current PID and then use that PID to find the process in the output of a subprocess calling top
/free
/ps
etc.
i'm not an OSX/BSD expert, so im unsure of the flags to which command will give you memory usage by process
MacosX has vmmap
and vmmap64
that you could use to access the information. I'm not python literate, but I imagine you should be able to open a pipe to a subprocess to read the output somehow.
EDIT: Some additional non-python information can be found here.
精彩评论