Class: RSpec::Memory::Trace
- Inherits:
-
Object
- Object
- RSpec::Memory::Trace
- Defined in:
- lib/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.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rspec/memory/trace.rb', line 62 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.
73 74 75 |
# File 'lib/rspec/memory/trace.rb', line 73 def allocated @allocated end |
#ignored ⇒ Object (readonly)
Returns the value of attribute ignored.
76 77 78 |
# File 'lib/rspec/memory/trace.rb', line 76 def ignored @ignored end |
#retained ⇒ Object (readonly)
Returns the value of attribute retained.
74 75 76 |
# File 'lib/rspec/memory/trace.rb', line 74 def retained @retained end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
78 79 80 |
# File 'lib/rspec/memory/trace.rb', line 78 def total @total end |
Class Method Details
.capture(*args, &block) ⇒ Object
49 50 51 52 53 |
# File 'lib/rspec/memory/trace.rb', line 49 def self.capture(*args, &block) self.new(*args).tap do |trace| trace.capture(&block) end end |
.supported? ⇒ Boolean
41 42 43 44 45 46 |
# File 'lib/rspec/memory/trace.rb', line 41 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
96 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 |
# File 'lib/rspec/memory/trace.rb', line 96 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
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rspec/memory/trace.rb', line 80 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
92 93 94 |
# File 'lib/rspec/memory/trace.rb', line 92 def find_base(object) @klasses.find{|klass| object.is_a? klass} end |