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.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/async/rspec/memory/trace.rb', line 60 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.
71 72 73 |
# File 'lib/async/rspec/memory/trace.rb', line 71 def allocated @allocated end |
#ignored ⇒ Object (readonly)
Returns the value of attribute ignored.
74 75 76 |
# File 'lib/async/rspec/memory/trace.rb', line 74 def ignored @ignored end |
#retained ⇒ Object (readonly)
Returns the value of attribute retained.
72 73 74 |
# File 'lib/async/rspec/memory/trace.rb', line 72 def retained @retained end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
76 77 78 |
# File 'lib/async/rspec/memory/trace.rb', line 76 def total @total end |
Class Method Details
.capture(&block) ⇒ Object
47 48 49 50 51 |
# File 'lib/async/rspec/memory/trace.rb', line 47 def self.capture(*args, &block) self.new(*args).tap do |trace| trace.capture(&block) end end |
.supported? ⇒ Boolean
42 43 44 |
# File 'lib/async/rspec/memory/trace.rb', line 42 def self.supported? ObjectSpace.respond_to? :trace_object_allocations end |
Instance Method Details
#capture(&block) ⇒ Object
94 95 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 |
# File 'lib/async/rspec/memory/trace.rb', line 94 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
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/async/rspec/memory/trace.rb', line 78 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
90 91 92 |
# File 'lib/async/rspec/memory/trace.rb', line 90 def find_base(object) @klasses.find{|klass| object.is_a? klass} end |