Class: MemoryProfiler::Reporter
- Inherits:
-
Object
- Object
- MemoryProfiler::Reporter
- Defined in:
- lib/memory_profiler/reporter.rb
Overview
Reporter is the top level API used for generating memory reports.
Instance Attribute Summary collapse
-
#top ⇒ Object
readonly
Returns the value of attribute top.
-
#trace ⇒ Object
readonly
Returns the value of attribute trace.
Class Method Summary collapse
-
.report(opts = {}, &block) ⇒ MemoryProfiler::Results
Helper for generating new reporter and running against block.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Reporter
constructor
A new instance of Reporter.
-
#run(&block) ⇒ Object
Collects object allocation and memory of ruby code inside of passed block.
Constructor Details
#initialize(opts = {}) ⇒ Reporter
Returns a new instance of Reporter.
12 13 14 15 16 17 |
# File 'lib/memory_profiler/reporter.rb', line 12 def initialize(opts = {}) @top = opts[:top] || 50 @trace = opts[:trace] && Array(opts[:trace]) @ignore_files = opts[:ignore_files] && Regexp.new(opts[:ignore_files]) @allow_files = opts[:allow_files] && /#{Array(opts[:allow_files]).join('|')}/ end |
Instance Attribute Details
#top ⇒ Object (readonly)
Returns the value of attribute top.
10 11 12 |
# File 'lib/memory_profiler/reporter.rb', line 10 def top @top end |
#trace ⇒ Object (readonly)
Returns the value of attribute trace.
10 11 12 |
# File 'lib/memory_profiler/reporter.rb', line 10 def trace @trace end |
Class Method Details
.report(opts = {}, &block) ⇒ MemoryProfiler::Results
Helper for generating new reporter and running against block.
26 27 28 |
# File 'lib/memory_profiler/reporter.rb', line 26 def self.report(opts={}, &block) self.new(opts).run(&block) end |
Instance Method Details
#run(&block) ⇒ Object
Collects object allocation and memory of ruby code inside of passed block.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/memory_profiler/reporter.rb', line 31 def run(&block) GC.start GC.disable generation = GC.count ObjectSpace.trace_object_allocations do block.call end allocated = object_list(generation) retained = StatHash.new.compare_by_identity GC.enable GC.start # Caution: Do not allocate any new Objects between the call to GC.start and the completion of the retained # lookups. It is likely that a new Object would reuse an object_id from a GC'd object. ObjectSpace.each_object do |obj| next unless ObjectSpace.allocation_generation(obj) == generation begin found = allocated[obj.__id__] retained[obj.__id__] = found if found rescue # __id__ is not defined on BasicObject, skip it # we can probably transplant the object_id at this point, # but it is quite rare end end ObjectSpace.trace_object_allocations_clear results = Results.new results.register_results(allocated, retained, top) results end |