The example in this paper describes how Python obtains system memory usage under Linux systems. Share to everyone for your reference. Specific as follows:
"" "Simple module for getting amount of memory used by a specified user's processes on a UNIX system. It uses UNIX PS utility to get the memory usage for a specified username and pipe it to awk for summing upper application Memory usage and return the total. Python ' s Popen () from subprocess module was used for spawning PS and awk. "" Import Subprocessclass Memorymonitor (object): def __init__ (self, username): "" " Create new Memorymonitor instance. "" " Self.username = Username def usage (self): "" " Return int containing memory used by user ' s processes. " " Self.process = subprocess. Popen ("Ps-u%s-o RSS | awk ' {sum+=$1} END {print sum} ' "% Self.username, shell=true, stdout=subprocess. PIPE, ) self.stdout_list = Self.process.communicate () [0].split (' \ n ') return int (self.stdout_list[0])
Save the above code as: memorymonitor.py
The calling method is as follows:
From memorymonitor Import Memorymonitormemory_mon = Memorymonitor (' username ') used_memory = Memory_mon.usage ()
Hopefully this article will help you with Python programming.