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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Reporter

Returns a new instance of Reporter.



16
17
18
19
20
21
# File 'lib/memory_profiler/reporter.rb', line 16

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

Class Attribute Details

.current_reporterObject

Returns the value of attribute current_reporter.



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

def current_reporter
  @current_reporter
end

Instance Attribute Details

#generationObject (readonly)

Returns the value of attribute generation.



14
15
16
# File 'lib/memory_profiler/reporter.rb', line 14

def generation
  @generation
end

#report_resultsObject (readonly)

Returns the value of attribute report_results.



14
15
16
# File 'lib/memory_profiler/reporter.rb', line 14

def report_results
  @report_results
end

#topObject (readonly)

Returns the value of attribute top.



14
15
16
# File 'lib/memory_profiler/reporter.rb', line 14

def top
  @top
end

#traceObject (readonly)

Returns the value of attribute trace.



14
15
16
# File 'lib/memory_profiler/reporter.rb', line 14

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:



30
31
32
# File 'lib/memory_profiler/reporter.rb', line 30

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.



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/memory_profiler/reporter.rb', line 65

def run(&block)
  start
  begin
    block.call
  rescue Exception
    ObjectSpace.trace_object_allocations_stop
    GC.enable
    raise
  else
    stop
  end
end

#startObject



34
35
36
37
38
39
40
# File 'lib/memory_profiler/reporter.rb', line 34

def start
  GC.start
  GC.disable

  @generation = GC.count
  ObjectSpace.trace_object_allocations_start
end

#stopObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/memory_profiler/reporter.rb', line 42

def stop
  ObjectSpace.trace_object_allocations_stop
  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
    found = allocated[obj.__id__]
    retained[obj.__id__] = found if found
  end
  ObjectSpace.trace_object_allocations_clear

  @report_results = Results.new
  @report_results.register_results(allocated, retained, top)
end