Skip to content

Instantly share code, notes, and snippets.

@robotmay
Last active December 31, 2015 03:19
Show Gist options
  • Save robotmay/7927068 to your computer and use it in GitHub Desktop.
Save robotmay/7927068 to your computer and use it in GitHub Desktop.
A Ruby utility script for use in `git bisect`. Returns a non-0 exit code if memory usage for the process exceeds a specified number.
# To run git bisect looking for the first spec suite to exceed 1000MB of RAM:
# git bisect run ruby memory_usage.rb 'rake spec' 1000000
require 'open3'
command = ARGV[0]
threshold = ARGV[1].to_i || 500000
cmd = "/usr/bin/time -v #{command}"
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
output = stderr.read.gsub("\t", "")
match = /Maximum resident set size \(kbytes\): (.*)/.match(output.strip)
memory_usage = match[1].to_i
p "Memory usage: #{memory_usage}KB"
if memory_usage > threshold
Process.exit 1
else
Process.exit 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment