Class: RSpec::Memory::Trace

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klasses) ⇒ Trace

Returns a new instance of Trace.



48
49
50
51
52
53
54
55
56
57
# File 'lib/rspec/memory/trace.rb', line 48

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

#allocatedObject (readonly)

Returns the value of attribute allocated.



59
60
61
# File 'lib/rspec/memory/trace.rb', line 59

def allocated
  @allocated
end

#ignoredObject (readonly)

Returns the value of attribute ignored.



62
63
64
# File 'lib/rspec/memory/trace.rb', line 62

def ignored
  @ignored
end

#retainedObject (readonly)

Returns the value of attribute retained.



60
61
62
# File 'lib/rspec/memory/trace.rb', line 60

def retained
  @retained
end

#totalObject (readonly)

Returns the value of attribute total.



64
65
66
# File 'lib/rspec/memory/trace.rb', line 64

def total
  @total
end

Class Method Details

.capture(*args, &block) ⇒ Object



35
36
37
38
39
# File 'lib/rspec/memory/trace.rb', line 35

def self.capture(*args, &block)
	self.new(*args).tap do |trace|
		trace.capture(&block)
	end
end

.supported?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
# File 'lib/rspec/memory/trace.rb', line 27

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



82
83
84
85
86
87
88
89
90
91
92
93
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
# File 'lib/rspec/memory/trace.rb', line 82

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



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rspec/memory/trace.rb', line 66

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



78
79
80
# File 'lib/rspec/memory/trace.rb', line 78

def find_base(object)
	@klasses.find{|klass| object.is_a? klass}
end