Class: Async::RSpec::Memory::Trace
- Inherits:
-
Object
- Object
- Async::RSpec::Memory::Trace
- Defined in:
- lib/async/rspec/memory/trace.rb
Instance Attribute Summary collapse
-
#allocated ⇒ Object
readonly
Returns the value of attribute allocated.
-
#ignored ⇒ Object
readonly
Returns the value of attribute ignored.
-
#retained ⇒ Object
readonly
Returns the value of attribute retained.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Class Method Summary collapse
Instance Method Summary collapse
- #capture(&block) ⇒ Object
- #current_objects(generation) ⇒ Object
- #find_base(object) ⇒ Object
-
#initialize(klasses) ⇒ Trace
constructor
A new instance of Trace.
Constructor Details
#initialize(klasses) ⇒ Trace
Returns a new instance of Trace.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/async/rspec/memory/trace.rb', line 63 def initialize(klasses) @klasses = klasses @allocated = Allocation.default_hash @retained = Allocation.default_hash @ignored = Allocation.default_hash @total = Allocation.new(0, 0) end |
Instance Attribute Details
#allocated ⇒ Object (readonly)
Returns the value of attribute allocated.
74 75 76 |
# File 'lib/async/rspec/memory/trace.rb', line 74 def allocated @allocated end |
#ignored ⇒ Object (readonly)
Returns the value of attribute ignored.
77 78 79 |
# File 'lib/async/rspec/memory/trace.rb', line 77 def ignored @ignored end |
#retained ⇒ Object (readonly)
Returns the value of attribute retained.
75 76 77 |
# File 'lib/async/rspec/memory/trace.rb', line 75 def retained @retained end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
79 80 81 |
# File 'lib/async/rspec/memory/trace.rb', line 79 def total @total end |
Class Method Details
.capture(*args, &block) ⇒ Object
50 51 52 53 54 |
# File 'lib/async/rspec/memory/trace.rb', line 50 def self.capture(*args, &block) self.new(*args).tap do |trace| trace.capture(&block) end end |
.supported? ⇒ Boolean
42 43 44 45 46 47 |
# File 'lib/async/rspec/memory/trace.rb', line 42 def self.supported? # There are issues on truffleruby-1.0.0rc9 return false if RUBY_ENGINE == "truffleruby" ObjectSpace.respond_to?(:trace_object_allocations) end |
Instance Method Details
#capture(&block) ⇒ Object
97 98 99 100 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 |
# File 'lib/async/rspec/memory/trace.rb', line 97 def capture(&block) GC.start begin GC.disable generation = GC.count ObjectSpace.trace_object_allocations(&block) allocated = current_objects(generation) ensure GC.enable end GC.start retained = current_objects(generation) # All allocated objects, including those freed in the last GC: allocated.each do |object| if klass = find_base(object) @allocated[klass] << object else # If the user specified classes, but we can't pin this allocation to a specific class, we issue a warning. if @klasses.any? warn "Ignoring allocation of #{object.class} at #{ObjectSpace.allocation_sourcefile(object)}:#{ObjectSpace.allocation_sourceline(object)}" end @ignored[object.class] << object end @total << object end # Retained objects are still alive after a final GC: retained.each do |object| if klass = find_base(object) @retained[klass] << object end end end |
#current_objects(generation) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/async/rspec/memory/trace.rb', line 81 def current_objects(generation) allocations = [] ObjectSpace.each_object do |object| if ObjectSpace.allocation_generation(object) == generation allocations << object end end return allocations end |
#find_base(object) ⇒ Object
93 94 95 |
# File 'lib/async/rspec/memory/trace.rb', line 93 def find_base(object) @klasses.find{|klass| object.is_a? klass} end |