Skip to content

Instantly share code, notes, and snippets.

@BlakeWilliams
Created September 4, 2021 01:49
Show Gist options
  • Save BlakeWilliams/f04b8654dbc34fb26c8ae3bffc2a2d07 to your computer and use it in GitHub Desktop.
Save BlakeWilliams/f04b8654dbc34fb26c8ae3bffc2a2d07 to your computer and use it in GitHub Desktop.
Track allocations by Ruby source file + line
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