-
-
Save JoshCheek/76dd9bc4cd326391a95704249f1d3341 to your computer and use it in GitHub Desktop.
Tracking allocated objects
This file contains hidden or 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
| echo Josh | ruby -r ./tracer.rb -e ' | |
| class User | |
| def initialize(id, name) | |
| @id, @name = id, name | |
| end | |
| def to_s | |
| "User #@id: #@name" | |
| end | |
| end | |
| User.new 123, gets.chomp | |
| omg | |
| ' |
This file contains hidden or 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_start | |
| def omg | |
| o = ObjectSpace | |
| all = o.each_object.to_a | |
| all.delete all | |
| pp all | |
| .select { o.allocation_class_path _1 } # ignore things we didn't see get allocated | |
| .select { o.allocation_method_id _1 } | |
| .group_by(&:class) | |
| .sort_by { _2.size }.to_h | |
| .transform_values { |objs| | |
| objs | |
| .map { "#{o.allocation_class_path _1}##{o.allocation_method_id _1}" } | |
| .tally | |
| .sort_by { _2 } | |
| .to_h | |
| } | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment