Class: Minitest::Memory::AllocationCounter

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

Overview

Counts object allocations within a block using ObjectSpace.

Defined Under Namespace

Classes: Allocation, Result

Constant Summary collapse

SLOT_SIZE =

Base memory size of an empty Ruby object slot.

ObjectSpace.memsize_of(Object.new)
EMPTY =

An empty allocation with zero count and size.

Allocation.new(0, 0, empty_sources.freeze).freeze

Class Method Summary collapse

Class Method Details

.count(klasses = []) ⇒ Object

Counts allocations by class within a block. Returns a Result. When klasses are given, objects are matched via is_a?; unmatched objects go to ignored. Temporarily disables GC during counting.



54
55
56
# File 'lib/minitest/memory.rb', line 54

def self.count(klasses = [], &)
  trace(klasses, &)
end

.count_allocations(generation, klasses = []) ⇒ Object

Returns a Result of allocations from the given generation. When klasses are given, objects are matched via is_a? and unmatched objects are tracked separately in ignored.



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/minitest/memory.rb', line 72

def self.count_allocations(generation, klasses = [])
  allocated = {} #: Hash[untyped, Allocation]
  ignored = {} #: Hash[untyped, Allocation]
  total = new_allocation

  ObjectSpace.each_object do |obj|
    next unless ObjectSpace.allocation_generation(obj) == generation

    tally(obj, total, bucket_for(obj, klasses, allocated, ignored))
  end

  Result.new(allocated, ignored, total)
end

.count_retained(klasses = []) ⇒ Object

Counts retained allocations by class within a block. Returns a Result. Runs GC after the block to identify objects that survive garbage collection.



63
64
65
# File 'lib/minitest/memory.rb', line 63

def self.count_retained(klasses = [], &)
  trace(klasses, retain: true, &)
end

.supported? ⇒ Boolean

Returns false on TruffleRuby where ObjectSpace tracing is not supported, true otherwise.

Returns:

  • (Boolean)


40
41
42
43
44
45
46
# File 'lib/minitest/memory.rb', line 40

def self.supported?
  # :nocov:
  return false if RUBY_ENGINE == "truffleruby"
  # :nocov:

  ObjectSpace.respond_to?(:trace_object_allocations)
end