Module: ObjectSpace::AllocationTracer

Defined in:
lib/allocation_tracer/version.rb,
lib/allocation_tracer.rb

Constant Summary collapse

VERSION =
"0.6.0"

Class Method Summary collapse

Class Method Details

.collect_lifetime_tableObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/allocation_tracer.rb', line 15

def self.collect_lifetime_table
  ObjectSpace::AllocationTracer.lifetime_table_setup true

  if block_given?
    begin
      ObjectSpace::AllocationTracer.trace do
        yield
      end
      result = ObjectSpace::AllocationTracer.lifetime_table
      output_lifetime_table(result)
    ensure
      ObjectSpace::AllocationTracer.lifetime_table_setup false
    end
  else
    ObjectSpace::AllocationTracer.trace
  end
end

.collect_lifetime_table_stopObject



33
34
35
36
37
38
39
# File 'lib/allocation_tracer.rb', line 33

def self.collect_lifetime_table_stop
  ObjectSpace::AllocationTracer.stop
  result = ObjectSpace::AllocationTracer.lifetime_table
  ObjectSpace::AllocationTracer.lifetime_table_setup false
  output_lifetime_table(result)
  result
end

.output_lifetime_table(table) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/allocation_tracer.rb', line 6

def self.output_lifetime_table table
  out = (file = ENV['RUBY_ALLOCATION_TRACER_LIFETIME_OUT']) ? open(File.expand_path(file), 'w') : STDOUT
  max_lines = table.inject(0){|r, (type, lines)| r < lines.size ? lines.size : r}
  out.puts "type\t" + (0...max_lines).to_a.join("\t")
  table.each{|type, line|
    out.puts "#{type}\t#{line.join("\t")}"
  }
end