Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created May 5, 2021 23:02
Show Gist options
  • Save JoshCheek/76dd9bc4cd326391a95704249f1d3341 to your computer and use it in GitHub Desktop.
Save JoshCheek/76dd9bc4cd326391a95704249f1d3341 to your computer and use it in GitHub Desktop.
Tracking allocated objects
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
'
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