Created
July 10, 2018 08:19
-
-
Save jpalumickas/92dccad4be1e06fa37f1abcc750e217a to your computer and use it in GitHub Desktop.
Check memory usage for Process in 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 '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