module ActiveProfiling::GCStatistics

Public Instance Methods

gc_statistics(*args) { || ... } click to toggle source

Profiles a block, capturing information on the garbage collector. The return value is an Array containing the result of the yielded block and a String with a report on the profiling results.

Options:

  • :disable_gc - disables garbage collection for the duration of the block and then renables it immediately afterwards. This allows you to control when GC is run and see the results.

  • :title - a title to use for logging.

More options for this method can be found in the default settings, located in ActiveProfiling::Railtie::DEFAULT_GC_STATISTICS_OPTIONS.

This method only works with versions of Ruby that implement GC::Profiler or that have been patched to implement some additional garbage collection statistics. In older versions, such as version 1.8.7, you can either use Ruby Enterprise Edition or patch your build with the GC statistics patch found here:

blog.pluron.com/2008/02/memory-profilin.html

# File lib/active-profiling/gc_statistics.rb, line 88
def gc_statistics(*args)
  options = Rails.application.config.active_profiling.gc_statistics.merge(args.extract_options!)

  result, gc_report = gc_statistics_report(options) do
    yield
  end

  ActiveSupport::Notifications.instrument('gc_statistics.active_profiling', {
    :report => gc_report,
    :title => options[:title] || args.first
  })

  result
end

Private Instance Methods

gc_statistics_report(options = {}) { || ... } click to toggle source
# File lib/active-profiling/gc_statistics.rb, line 7
def gc_statistics_report(options = {})
  options = {
    :disable_gc => false
  }.merge(options)

  GC.disable if options[:disable_gc]
  GC::Profiler.enable
  GC::Profiler.clear

  retval = yield

  if options[:disable_gc]
    GC.enable
  end

  result = GC::Profiler.result

  return [ retval, result ]

ensure
  GC.enable if options[:disable_gc]
  GC::Profiler.disable
  GC::Profiler.clear
end