Last active
October 15, 2017 19:33
-
-
Save tfiers/013fe3048df900ff3804fa3a6e4d4d3f to your computer and use it in GitHub Desktop.
How much memory does Chrome use in total? Python script to find this out on a Windows machine.
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
# coding: utf-8 | |
# | |
# Montiors the total memory usage of all Chrome processes. | |
# To be run on a Windows machine. | |
# Requirements: | |
# - Python 3.6+ | |
# - Pandas | |
print('Press Ctrl-C to quit') | |
print('\n..', end='') | |
import pandas as pd | |
import subprocess | |
from io import StringIO | |
from time import sleep | |
def get_total_chrome_usage(): | |
# Call Windows command `tasklist` to get Task Manager data. | |
out = subprocess.check_output('tasklist') | |
# Decode binary data to a string, and convert to a file-like object so Pandas | |
# can read it. | |
data = StringIO(out.decode('utf-8')) | |
# Parse tabular data to a DataFrame using Pandas | |
df = pd.read_csv(data, sep='\s\s+', skiprows=[2], engine='python') | |
# Select only the Chrome rows | |
chromes = df[df['Image Name'] == 'chrome.exe'] | |
def parse_mem(s): | |
''' Converts a 'Mem Usage' string to a float, with units [MB] ''' | |
return int(s[:-2].replace(',', '')) / 1e3 | |
# Transform the 'Mem Usage' column of strings to a column of floats | |
mem = chromes['Mem Usage'].transform(parse_mem) | |
# Sum to get Chrome's total memory usage | |
return mem.sum() | |
if __name__ == '__main__': | |
try: | |
while True: | |
usage = get_total_chrome_usage() | |
# The carriage return '\r' brings us back to the beginning of the line. | |
print(f'\rChrome is eating {usage / 1e3:.3g} GB of memory ', end='') | |
sleep(0.5) | |
except KeyboardInterrupt: | |
print('\nBye') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ubipo
(Install
Pandas
using Conda:conda install pandas
)