Skip to content

Instantly share code, notes, and snippets.

@frankolson
Last active May 4, 2018 17:45
Show Gist options
  • Save frankolson/366fe419f00413d263863e8a01715341 to your computer and use it in GitHub Desktop.
Save frankolson/366fe419f00413d263863e8a01715341 to your computer and use it in GitHub Desktop.
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