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



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

def pretty_print(io = STDOUT, **options)
  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)"

  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

  io.puts
  dump_strings(io, "Allocated", strings_allocated)
  io.puts
  dump_strings(io, "Retained", strings_retained)

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

def string_report(data, top)
  data.values
      .keep_if { |stat| stat.string_value }
      .map! { |stat| [stat.string_value, stat.location] }
      .group_by { |string, _location| string }
      .sort_by {|string, list| [-list.size, string] }
      .first(top)
      .map { |string, list| [string, list.group_by { |_string, location| location }
                                         .map { |location, locations| [location, locations.size] }
                            ]
      }
end