Skip to content

Instantly share code, notes, and snippets.

@tfiers
Last active October 15, 2017 19:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tfiers/013fe3048df900ff3804fa3a6e4d4d3f to your computer and use it in GitHub Desktop.
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.
# 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')
@tfiers
Copy link
Author

tfiers commented Oct 15, 2017

@ubipo

(Install Pandas using Conda: conda install pandas)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment