Skip to content

Instantly share code, notes, and snippets.

@cghawthorne
Created July 5, 2010 19:32
Show Gist options
  • Save cghawthorne/464624 to your computer and use it in GitHub Desktop.
Save cghawthorne/464624 to your computer and use it in GitHub Desktop.
# Add this to your app/controllers/application_controller.rb
# Log GC stats, if enabled (make this the first before_filter)
before_filter :before_log_gc_stats
# Log GC stats, if enabled (make this the last after_filter)
after_filter :after_log_gc_stats
def before_log_gc_stats
return if (ENV['RUBY_GC_STATS'].nil? or ENV['RUBY_GC_STATS'] != '1')
GC.log "***"
GC.log "Request starting: " + request.url
GC.log "***"
end
def after_log_gc_stats
return if (ENV['RUBY_GC_STATS'].nil? or ENV['RUBY_GC_STATS'] != '1')
GC.log "***"
GC.log "Request completed: " + request.url
GC.log "GC collections: " + GC.collections.to_s
GC.log "GC time (us): " + GC.time.to_s
GC.log "Bytes allocated since last GC run: " + GC.growth.to_s
GC.log "Bytes allocated since last request: " + GC.allocated_size.to_s
GC.log "Number of allocations since last request: " + GC.num_allocations.to_s
GC.log "Live objects: " + ObjectSpace.live_objects.to_s
GC.log "Total allocated objects since interpreter start: " + ObjectSpace.allocated_objects.to_s
GC.dump
GC.log "***"
GC.clear_stats
end
# Add this file to your config/initializers directory
# To enable logging of Ruby garbage collector and memory allocation stats
# with Ruby Enterprise Edition, set these environment variables:
#
# RUBY_GC_STATS=1
# RUBY_GC_DATA_FILE=/tmp/ruby_gc.txt
if !ENV['RUBY_GC_STATS'].nil? and ENV['RUBY_GC_STATS'] == '1' then
GC.enable_stats
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment