Class: MemoryProfiler::Results

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#strings_allocatedObject

Returns the value of attribute strings_allocated.



18
19
20
# File 'lib/memory_profiler/results.rb', line 18

def strings_allocated
  @strings_allocated
end

#strings_retainedObject

Returns the value of attribute strings_retained.



18
19
20
# File 'lib/memory_profiler/results.rb', line 18

def strings_retained
  @strings_retained
end

#total_allocatedObject

Returns the value of attribute total_allocated.



19
20
21
# File 'lib/memory_profiler/results.rb', line 19

def total_allocated
  @total_allocated
end

#total_allocated_memsizeObject

Returns the value of attribute total_allocated_memsize.



20
21
22
# File 'lib/memory_profiler/results.rb', line 20

def total_allocated_memsize
  @total_allocated_memsize
end

#total_retainedObject

Returns the value of attribute total_retained.



19
20
21
# File 'lib/memory_profiler/results.rb', line 19

def total_retained
  @total_retained
end

#total_retained_memsizeObject

Returns the value of attribute total_retained_memsize.



20
21
22
# File 'lib/memory_profiler/results.rb', line 20

def total_retained_memsize
  @total_retained_memsize
end

Class Method Details

.register_type(name, stat_attribute) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/memory_profiler/results.rb', line 4

def self.register_type(name, stat_attribute)
  @@lookups ||= []
  @@lookups << [name, stat_attribute]

  ["allocated", "retained"].product(["objects", "memory"]).each do |type, metric|
    attr_accessor "#{type}_#{metric}_by_#{name}"
  end
end

Instance Method Details

#pretty_print(io = $stdout, **options) ⇒ Object

Output the results of the report

Parameters:

  • options (Hash)

    the options for output

  • opts (Hash)

    a customizable set of options



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/memory_profiler/results.rb', line 79

def pretty_print(io = $stdout, **options)
  # Handle the special case that Ruby PrettyPrint expects `pretty_print`
  # to be a customized pretty printing function for a class
  return io.pp_object(self) if defined?(PP) && io.is_a?(PP)

  io = File.open(options[:to_file], "w") if options[:to_file]

  color_output = options.fetch(:color_output) { io.respond_to?(:isatty) && io.isatty }
  @colorize = color_output ? Polychrome.new : Monochrome.new

  io.puts "Total allocated: #{total_allocated_memsize} bytes (#{total_allocated} objects)"
  io.puts "Total retained:  #{total_retained_memsize} bytes (#{total_retained} objects)"

  if options[:detailed_report] != false
    io.puts
    ["allocated", "retained"]
        .product(["memory", "objects"])
        .product(["gem", "file", "location", "class"])
        .each do |(type, metric), name|
          dump "#{type} #{metric} by #{name}", self.send("#{type}_#{metric}_by_#{name}"), io
        end
  end

  io.puts
  dump_strings(io, "Allocated", strings_allocated, limit: options[:allocated_strings])
  io.puts
  dump_strings(io, "Retained", strings_retained, limit: options[:retained_strings])

  io.close if io.is_a? File
end

#register_results(allocated, retained, top) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/memory_profiler/results.rb', line 22

def register_results(allocated, retained, top)

  @@lookups.each do |name, stat_attribute|

    memsize_results, count_results = allocated.top_n(top, stat_attribute)

    self.send("allocated_memory_by_#{name}=", memsize_results)
    self.send("allocated_objects_by_#{name}=", count_results)

    memsize_results, count_results = retained.top_n(top, stat_attribute)

    self.send("retained_memory_by_#{name}=", memsize_results)
    self.send("retained_objects_by_#{name}=", count_results)
  end


  self.strings_allocated = string_report(allocated, top)
  self.strings_retained = string_report(retained, top)

  self.total_allocated = allocated.size
  self.total_allocated_memsize = allocated.values.map!(&:memsize).inject(0, :+)
  self.total_retained = retained.size
  self.total_retained_memsize = retained.values.map!(&:memsize).inject(0, :+)

  self
end

#string_report(data, top) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/memory_profiler/results.rb', line 49

def string_report(data, top)
  grouped_strings = data.values.
    keep_if { |stat| stat.string_value }.
    group_by { |stat| stat.string_value.object_id }.
    values

  if grouped_strings.size > top
    cutoff = grouped_strings.sort_by!(&:size)[-top].size
    grouped_strings.keep_if { |list| list.size >= cutoff }
  end

  grouped_strings.
    sort! { |a, b| a.size == b.size ? a[0].string_value <=> b[0].string_value : b.size <=> a.size }.
    first(top).
    # Return array of [string, [[location, count], [location, count], ...]
    map! { |list| [list[0].string_value,
                   list.group_by { |stat| stat.location }.
                        map { |location, stat_list| [location, stat_list.size] }.
                        sort_by!(&:last).reverse!
                  ]
    }
end