Class: Memprof2
- Inherits:
-
Object
- Object
- Memprof2
- Defined in:
- lib/memprof2.rb
Class Method Summary collapse
- .report(opts = {}) ⇒ Object
- .report!(opts = {}) ⇒ Object
- .run(&block) ⇒ Object
- .run_with_report(opts = {}, &block) ⇒ Object
- .start ⇒ Object
- .stop ⇒ Object
Instance Method Summary collapse
Class Method Details
.report(opts = {}) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/memprof2.rb', line 18 def report(opts = {}) ObjectSpace.trace_object_allocations_stop self.new.report(opts) ensure ObjectSpace.trace_object_allocations_start end |
.report!(opts = {}) ⇒ Object
25 26 27 28 |
# File 'lib/memprof2.rb', line 25 def report!(opts = {}) report(opts) ObjectSpace.trace_object_allocations_clear end |
.run(&block) ⇒ Object
14 15 16 |
# File 'lib/memprof2.rb', line 14 def run(&block) ObjectSpace.trace_object_allocations(&block) end |
.run_with_report(opts = {}, &block) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/memprof2.rb', line 30 def run_with_report(opts = {}, &block) start yield report(opts) ensure stop end |
.start ⇒ Object
5 6 7 |
# File 'lib/memprof2.rb', line 5 def start ObjectSpace.trace_object_allocations_start end |
.stop ⇒ Object
9 10 11 12 |
# File 'lib/memprof2.rb', line 9 def stop ObjectSpace.trace_object_allocations_stop ObjectSpace.trace_object_allocations_clear end |
Instance Method Details
#collect_info ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/memprof2.rb', line 61 def collect_info results = {} ObjectSpace.each_object do |o| next unless (file = ObjectSpace.allocation_sourcefile(o)) next if file == __FILE__ next if (@trace and @trace !~ file) next if (@ignore and @ignore =~ file) line = ObjectSpace.allocation_sourceline(o) if RUBY_VERSION >= "2.2.0" # Ruby 2.2.0 takes into account sizeof(RVALUE) # https://bugs.ruby-lang.org/issues/8984 memsize = ObjectSpace.memsize_of(o) else # Ruby < 2.2.0 does not have ways to get memsize correctly, but let me make a bid as much as possible # https://twitter.com/sonots/status/544334978515869698 memsize = ObjectSpace.memsize_of(o) + @rvalue_size end memsize = @rvalue_size if memsize > 100_000_000_000 # compensate for API bug klass = o.class.name rescue "BasicObject" location = "#{file}:#{line}:#{klass}" results[location] ||= 0 results[location] += memsize end results end |
#configure(opts = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/memprof2.rb', line 49 def configure(opts = {}) @rvalue_size = GC::INTERNAL_CONSTANTS[:RVALUE_SIZE] if @trace = opts[:trace] raise ArgumentError, "`trace` option must be a Regexp object" unless @trace.is_a?(Regexp) end if @ignore = opts[:ignore] raise ArgumentError, "`ignore` option must be a Regexp object" unless @ignore.is_a?(Regexp) end @out = opts[:out] || "/dev/stdout" self end |
#report(opts = {}) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/memprof2.rb', line 39 def report(opts={}) configure(opts) results = collect_info File.open(@out, 'a') do |io| results.each do |location, memsize| io.puts "#{memsize} #{location}" end end end |