Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
module MemoryLogger
def log_memory(&block)
begin
log_start
thread = create_memory_log_thread
yield
ensure
Thread.kill(thread)
log_finish
end
end
private
def start_time
@start_time ||= Time.now
end
def log_start
Rails.logger.info "[MemoryLogger] Start time: #{start_time}"
end
def create_memory_log_thread
Thread.new do
while true
rss = `ps -eo pid,rss | grep #{$$} | awk '{print $2}'`.to_i
Rails.logger.info "[MemoryLogger] Resident memory size: #{rss * 0.001}Mb"
sleep 5
end
end
end
def log_finish
Rails.logger.info "[MemoryLogger] Completed in: #{(Time.now - start_time) / 60} minutes"
end
end
require 'memory_logger'
def foo_bar
# do some cool stuff...
end
log_memory do
foo_bar
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.