Class: MemoryProfiler::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/memory_profiler/reporter.rb

Overview

Reporter is the top level API used for generating memory reports.

Examples:

Measure object allocation in a block

report = Reporter.report(top: 50) do
  5.times { "foo" }
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#topObject (readonly)

Returns the value of attribute top.



10
11
12
# File 'lib/memory_profiler/reporter.rb', line 10

def top
  @top
end

#traceObject (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.

Parameters:

  • opts (Hash) (defaults to: {})

    the options to create a report with

Options Hash (opts):

  • :top (Object)

    max number of entries to output

  • :trace (Object)

    a class or an array of classes you explicitly want to trace

  • :ignore_files (Object)

    a regular expression used to exclude certain files from tracing

  • :allow_files (Object)

    a string or array of strings to selectively include in tracing

Returns:



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