ObjectSpaceダンプ

投稿者 nanki 2011-02-17 14:03:00 GMT
def top_objects(num=20)
  ObjectSpace.each_object.inject(Hash.new{0}){|h,o|h[o.class]+=1;h}.sort_by{|k,v|-v}.first(num).each{|k,v|puts "#{k}\t#{v}"}
end

GC.start
top_objects

--
String  577159
Array  163891
Hash  93468
Time  50353
ActiveSupport::TimeWithZone  50129
ActiveModel::Errors  23365
TZInfo::TimezonePeriod  23340
ActiveSupport::HashWithIndifferentAccess  23236
RubyVM::InstructionSequence  12484
MIME::Type  1197
MatchData  1183
ActiveModel::AttributeMethods::ClassMethods::AttributeMethodMatcher::AttributeMethodMatch  1128
Class  1101
Regexp  972
Proc  704
Module  655
Gem::Version  619

GCを生き残ったインスタンス数をダンプ。