Skip to content

Instantly share code, notes, and snippets.

@EvAlex
Last active April 20, 2017 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EvAlex/6d7bec1bafc846a6dfbee79deb1d1dfc to your computer and use it in GitHub Desktop.
Save EvAlex/6d7bec1bafc846a6dfbee79deb1d1dfc to your computer and use it in GitHub Desktop.
Get Process RAM usage in Windows - Ruby
require 'CSV';
def get_ram_usage(pid)
output = CSV.parse(`tasklist /FO CSV /V /FI "PID eq #{pid}"`)
# Column name is "Mem Usage". Usually it's 5-th column (index 4)
col_index = output[0].each_index.select { |i| output[0][i] =~ /mem/i }.first || 4
parse_usage_str output[1][col_index]
end
def parse_usage_str(str)
str.strip!
multiplier = 1024 if str =~ /kb?$/i
multiplier = 1024 * 1024 if str =~ /mb?$/i
str.gsub! /[\skmb]+/i, ''
str.gsub! ',', '.'
str.to_f * multiplier
end
get_ram_usage Process.pid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment