Skip to content

Instantly share code, notes, and snippets.

@woahdae
Created January 17, 2009 23:02
Show Gist options
  • Save woahdae/48479 to your computer and use it in GitHub Desktop.
Save woahdae/48479 to your computer and use it in GitHub Desktop.
def profile(unit = :M)
# trying to see where our memory is going
population = Hash.new{|h,k| h[k] = [0,0]}
array_sizes = Hash.new{|h,k| h[k] = 0}
ObjectSpace.each_object do |object|
# rough estimates, see http://eigenclass.org/hiki.rb?ruby+space+overhead
size = case object
when Array
array_sizes[object.size / 10] += 1
case object.size
when 0..16
20 + 64
else
20 + 4 * object.size * 1.5
end
when Hash; 40 + 4 * [object.size / 5, 11].max + 16 * object.size
when String; 30 + object.size
else 120 # the iv_tbl, etc
end
count, tsize = population[object.class]
population[object.class] = [count + 1, tsize + size]
end
population.sort_by{|k,(c,s)| s}.reverse[0..10].each do |klass, (count, bytes)|
case unit
when :M
puts "%-20s %7d %9f" % [klass, count, bytes/1048576]
when :B
puts "%-20s %7d %9d" % [klass, count, bytes]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment