Skip to content

Instantly share code, notes, and snippets.

@lmars
Last active December 29, 2015 10:19
Show Gist options
  • Save lmars/7656681 to your computer and use it in GitHub Desktop.
Save lmars/7656681 to your computer and use it in GitHub Desktop.
Dump top ten Ruby objects by count every minute
Thread.start do
loop do
objects = Hash.new(0)
ObjectSpace.each_object { |obj| objects[obj.class] += 1 }
File.open("/tmp/top_ten_objects.txt", "a") do |file|
file.puts "-" * 50
file.puts "PID: #{Process.pid} - #{Time.now.strftime("%c")}"
file.puts "-" * 50
Hash[objects.sort_by { |k,v| -v }.take(10)].each_pair do |klass, count|
file.puts "#{klass.name}: #{count}"
end
end
sleep 60
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment