Last active
April 20, 2017 11:37
-
-
Save EvAlex/6d7bec1bafc846a6dfbee79deb1d1dfc to your computer and use it in GitHub Desktop.
Get Process RAM usage in Windows - Ruby
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
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