Class: HeapProfiler::Reporter

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

Instance Method Summary collapse

Constructor Details

#initialize(dir_path) ⇒ Reporter

Returns a new instance of Reporter.



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

def initialize(dir_path)
  @dir_path = dir_path
  @enable_tracing = !allocation_tracing_enabled?
  @generation = nil
  @partial = true
end

Instance Method Details

#runObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/heap_profiler/reporter.rb', line 72

def run
  start
  begin
    yield
  rescue Exception
    ObjectSpace.trace_object_allocations_stop if @enable_tracing
    GC.enable
    raise
  else
    stop
  end
end

#start(partial: true) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/heap_profiler/reporter.rb', line 41

def start(partial: true)
  @partial = partial
  FileUtils.mkdir_p(@dir_path)
  ObjectSpace.trace_object_allocations_start if @enable_tracing

  @allocated_heap = open_heap("allocated")
  @retained_heap = open_heap("retained")

  HeapProfiler.name_anonymous_modules!

  GC.start
  GC.disable
  @generation = GC.count
end

#stopObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/heap_profiler/reporter.rb', line 56

def stop
  HeapProfiler.name_anonymous_modules!
  ObjectSpace.trace_object_allocations_stop if @enable_tracing

  # we can't use partial dump for allocated.heap, because we need old generations
  # as well to build the classes and strings indexes.
  dump_heap(@allocated_heap)

  GC.enable
  GC.start
  dump_heap(@retained_heap, partial: @partial)
  @allocated_heap.close
  @retained_heap.close
  write_info("generation", @partial ? @generation.to_s : "0")
end