Skip to content

Instantly share code, notes, and snippets.

@piotrze
Created September 25, 2014 08:41
Show Gist options
  • Save piotrze/e53817ccf9218af22742 to your computer and use it in GitHub Desktop.
Save piotrze/e53817ccf9218af22742 to your computer and use it in GitHub Desktop.
Count number of objects created per request
#app/controllers/application_controller.rb
around_filter :log_objects_creation
protected
def log_objects_creation
started = ObjectSpace.count_objects[:T_OBJECT]
yield
elapsed = ObjectSpace.count_objects[:T_OBJECT] - started
MemoryLogger.info("#{Time.now}| #{elapsed}| #{params}| #{current_user.nil? ? '' : current_user.id}") if elapsed > 2000
end
#config/initializers/memory_logger.rb
MemoryLogger = Logger.new("#{Rails.root}/log/ruby_memory.log")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment