Created
September 4, 2021 01:49
-
-
Save BlakeWilliams/f04b8654dbc34fb26c8ae3bffc2a2d07 to your computer and use it in GitHub Desktop.
Track allocations by Ruby source file + line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "objspace" | |
ObjectSpace.trace_object_allocations do | |
yield | |
end | |
allocations = ObjectSpace.each_object.map do |obj| | |
next if ObjectSpace.allocation_sourcefile(obj).nil? | |
next if ObjectSpace.allocation_sourcefile(obj) == __FILE__ | |
obj.class.to_s + " - " + ObjectSpace.allocation_sourcefile(obj) + ":" + ObjectSpace.allocation_sourceline(obj).to_s | |
end.compact | |
allocations.tally.each do |k, v| | |
puts "#{v}: #{k}" | |
end | |
ObjectSpace.trace_object_allocations_clear |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment