Class: Minitest::Memory::AllocationCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/minitest/memory.rb

Overview

Counts object allocations within a block using ObjectSpace.

Class Method Summary collapse

Class Method Details

.countObject

Counts allocations by class within a block. Returns a Hash mapping each class to its allocation count. Temporarily disables GC during counting.



21
22
23
24
25
26
27
28
29
# File 'lib/minitest/memory.rb', line 21

def self.count(&)
  GC.start
  GC.disable
  generation = GC.count
  ObjectSpace.trace_object_allocations(&)
  count_allocations generation
ensure
  GC.enable
end

.count_allocations(generation) ⇒ Object

Returns a Hash of allocations from the given generation.



34
35
36
37
38
39
40
# File 'lib/minitest/memory.rb', line 34

def self.count_allocations generation
  allocations = Hash.new(0)
  ObjectSpace.each_object do |obj|
    allocations[obj.class] += 1 if ObjectSpace.allocation_generation(obj) == generation
  end
  allocations
end