Created
November 5, 2015 12:49
-
-
Save grantc/f76aa2beec41800ee719 to your computer and use it in GitHub Desktop.
list top 20 proceses by memory usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import psutil | |
import json | |
import datetime | |
procs = [] | |
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
for proc in psutil.process_iter(): | |
try: | |
pinfo = proc.as_dict(attrs=['pid', 'name', 'cmdline']) | |
except psutil.NoSuchProcess: | |
pass | |
else: | |
try: | |
mem = proc.memory_info_ex().rss | |
except AttributeError: | |
mem = 0 | |
pass | |
if mem > 0: | |
procs.append((pinfo['name'], pinfo['pid'], mem)) | |
procs=sorted(procs, key=lambda process: process[2], reverse=True) | |
output = json.dumps({'date': timestamp, 'list': procs[0:20]}) | |
print output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment