Class: MemoryProfiler::Results

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

Constant Summary collapse

UNIT_PREFIXES =
{
  0 => 'B',
  3 => 'kB',
  6 => 'MB',
  9 => 'GB',
  12 => 'TB',
  15 => 'PB',
  18 => 'EB',
  21 => 'ZB',
  24 => 'YB'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#strings_allocatedObject

Returns the value of attribute strings_allocated.



31
32
33
# File 'lib/memory_profiler/results.rb', line 31

def strings_allocated
  @strings_allocated
end

#strings_retainedObject

Returns the value of attribute strings_retained.



31
32
33
# File 'lib/memory_profiler/results.rb', line 31

def strings_retained
  @strings_retained
end

#total_allocatedObject

Returns the value of attribute total_allocated.



32
33
34
# File 'lib/memory_profiler/results.rb', line 32

def total_allocated
  @total_allocated
end

#total_allocated_memsizeObject

Returns the value of attribute total_allocated_memsize.



33
34
35
# File 'lib/memory_profiler/results.rb', line 33

def total_allocated_memsize
  @total_allocated_memsize
end

#total_retainedObject

Returns the value of attribute total_retained.



32
33
34
# File 'lib/memory_profiler/results.rb', line 32

def total_retained
  @total_retained
end

#total_retained_memsizeObject

Returns the value of attribute total_retained_memsize.



33
34
35
# File 'lib/memory_profiler/results.rb', line 33

def total_retained_memsize
  @total_retained_memsize
end

Class Method Details

.register_type(name, stat_attribute) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/memory_profiler/results.rb', line 17

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/memory_profiler/results.rb', line 101

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

  if options[:scale_bytes]
    total_allocated_output = scale_bytes(total_allocated_memsize)
    total_retained_output = scale_bytes(total_retained_memsize)
  else
    total_allocated_output = "#{total_allocated_memsize} bytes"
    total_retained_output = "#{total_retained_memsize} bytes"
  end

  io.puts "Total allocated: #{total_allocated_output} (#{total_allocated} objects)"
  io.puts "Total retained:  #{total_retained_output} (#{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|
          scale_data = metric == "memory" && options[:scale_bytes]
          dump "#{type} #{metric} by #{name}", self.send("#{type}_#{metric}_by_#{name}"), io, scale_data
        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



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
# File 'lib/memory_profiler/results.rb', line 35

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

#scale_bytes(bytes) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/memory_profiler/results.rb', line 62

def scale_bytes(bytes)
  return "0 B" if bytes.zero?

  scale = Math.log10(bytes).div(3) * 3
  scale = 24 if scale > 24
  "#{(bytes / 10.0**scale).round(2)} #{UNIT_PREFIXES[scale]}"
end

#string_report(data, top) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/memory_profiler/results.rb', line 70

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