Skip to content

Instantly share code, notes, and snippets.

@jpalumickas
Created July 10, 2018 08:19
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 jpalumickas/92dccad4be1e06fa37f1abcc750e217a to your computer and use it in GitHub Desktop.
Save jpalumickas/92dccad4be1e06fa37f1abcc750e217a to your computer and use it in GitHub Desktop.
Check memory usage for Process in Ruby
require 'benchmark'
def print_memory_usage
memory_before = `ps -o rss= -p #{Process.pid}`.to_i
yield
memory_after = `ps -o rss= -p #{Process.pid}`.to_i
puts "Memory: #{((memory_after - memory_before) / 1024.0).round(2)} MB"
end
def print_time_spent
time = Benchmark.realtime do
yield
end
puts "Time: #{time.round(2)}"
end
print_memory_usage do
print_time_spent do
lines = []
10.times.each { lines << 'line' }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment