Created
September 13, 2020 14:07
-
-
Save bartman/0e976aaf3087e420c97ae4c5d850af04 to your computer and use it in GitHub Desktop.
kill-big-chrome-renderers.py
This file contains hidden or 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
#!/usr/bin/env python3 | |
# https://superuser.com/questions/413349/limiting-use-of-ram-in-chrome | |
import sys, os, psutil | |
limit = 200 #default 200MB | |
if len(sys.argv) == 2: | |
try: | |
limit = int(sys.argv[1]) | |
except: | |
print("parse of '%s' failed", sys.argv[1]) | |
sys.exit(1) | |
uid = os.getuid() | |
for proc in psutil.process_iter(['pid','cmdline','name','uids']): | |
try: | |
pi = proc.info | |
if not pi['name'] == 'chrome': | |
continue | |
if not uid in pi['uids']: | |
continue | |
if not any('type=renderer' in part for part in pi['cmdline']): | |
continue | |
mi = proc.memory_info() | |
rss = mi.rss >> 20 | |
if rss > limit: # kill if rss is greater than limit | |
#print("---") | |
#print(pi) | |
#print(mi) | |
print('Killed %d (rss %d MB)' % (pi['pid'], rss)) | |
proc.kill() | |
except psutil.Error.NoSuchProcess: | |
pass | |
except psutil.Error.AccessDenied: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment