免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2755 | 回复: 3
打印 上一主题 下一主题

ruby memory profile [复制链接]

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-07-05 17:44 |只看该作者 |倒序浏览
  1. include ObjectSpace

  2. MEMORY_PROFILE_BAD_SIZE_METHOD = {FileTest => true, File => true, File::Stat => true}

  3. class Object
  4.   def memory_profile_size_of_object(seen={})
  5.     return 0 if seen.has_key? object_id
  6.     seen[object_id] = true
  7.     count = 1
  8.     if kind_of? Hash
  9.       each_pair do |key,value|
  10.         count += key.memory_profile_size_of_object(seen)
  11.         count += value.memory_profile_size_of_object(seen)
  12.       end
  13.     elsif kind_of? Array
  14.       count += size
  15.       each do |element|
  16.         count += element.memory_profile_size_of_object(seen)
  17.       end
  18.     end

  19.     count += instance_variables.size
  20.     instance_variables.each do |var|
  21.       count += instance_variable_get(var.to_sym).memory_profile_size_of_object(seen)
  22.     end

  23.     count
  24.   end

  25.   def memory_profile_inspect(seen={},level=0)
  26.     return object_id.to_s if seen.has_key? object_id
  27.     seen[object_id] = true
  28.     result = ' '*level
  29.     if kind_of? Hash
  30.       result += "{\n" + ' '*level
  31.       each_pair do |key,value|
  32.         result += key.memory_profile_inspect(seen,level+1) + "=>\n"
  33.         result += value.memory_profile_inspect(seen,level+2) + ",\n" + ' '*level
  34.       end
  35.       result += "}\n" + ' '*level
  36.     elsif kind_of? Array
  37.       result += "[\n" + ' '*level
  38.       each do |element|
  39.         result += element.memory_profile_inspect(seen,level+1) + ",\n" + ' '*level
  40.       end
  41.       result += "]\n" + ' '*level
  42.     elsif kind_of? String
  43.       result += self
  44.     elsif kind_of? Numeric
  45.       result += self.to_s
  46.     elsif kind_of? Class
  47.       result += to_s
  48.     else
  49.       result += "---"+self.class.to_s + "---\n" + ' '*level
  50.     end


  51.     instance_variables.each do |var|
  52.       result += var + "=" + instance_variable_get(var.to_sym).memory_profile_inspect(seen,level+1) + "\n" + ' '*level
  53.     end

  54.     result
  55.   end

  56. end

  57. module MemoryProfile
  58.   LOG_FILE = "/tmp/memory_profile.log"

  59.   def MemoryProfile::report
  60.     Dir.chdir "/tmp"
  61.     ObjectSpace::garbage_collect
  62.     sleep 10 # Give the GC thread a chance
  63.     all = []
  64.     ObjectSpace.each_object do |obj|
  65.       next if obj.object_id == all.object_id
  66.         
  67.       all << obj
  68.     end
  69.    
  70.     tally = Hash.new(0)
  71.     max_obj = nil
  72.     max_count = 0
  73.     all.each do |obj|
  74.       count = obj.memory_profile_size_of_object
  75.       if max_count < count
  76.         max_obj = obj
  77.         max_count = count
  78.       end
  79.       
  80.       tally[obj.class]+=count
  81.     end
  82.    
  83.     open( LOG_FILE, 'a') do |outf|
  84.       outf.puts '+'*70
  85.       tally.keys.sort{|a,b|
  86.         if tally[a] == tally[b]
  87.           a.to_s <=> b.to_s
  88.         else
  89.           -1*(tally[a]<=>tally[b])
  90.         end
  91.       }.each do |klass|
  92.         outf.puts "#{klass}\t#{tally[klass]}"
  93.       end
  94.       
  95.       outf.puts '-'*70
  96.       outf.puts "Max obj was #{max_obj.class} at #{max_count}"
  97.       outf.puts "Maximum object is..."
  98.       outf.puts max_obj.memory_profile_inspect
  99.     end
  100.   end

  101.   def MemoryProfile::simple_count
  102.     Dir.chdir "/tmp"
  103.     ObjectSpace::garbage_collect
  104.     sleep 10 # Give the GC thread a chance

  105.     tally = Hash.new(0)
  106.     ObjectSpace.each_object do |obj|
  107.       next if obj.object_id == tally.object_id
  108.       tally[obj.class]+=1
  109.     end
  110.    
  111.     open( LOG_FILE, 'a') do |outf|
  112.       outf.puts '='*70
  113.       outf.puts "MemoryProfile report for #{$0}"
  114.       outf.puts `cat /proc/#{Process.pid}/status`
  115.       
  116.       tally.keys.sort{|a,b|
  117.         if tally[a] == tally[b]
  118.           a.to_s <=> b.to_s
  119.         else
  120.           -1*(tally[a]<=>tally[b])
  121.         end
  122.       }.each do |klass|
  123.         outf.puts "#{klass}\t#{tally[klass]}"
  124.       end
  125.     end
  126.   end
  127. end

  128. if $0 == __FILE__ then
  129.   File.unlink MemoryProfile::LOG_FILE if FileTest.exist? MemoryProfile::LOG_FILE

  130.   at_exit{ system("cat #{MemoryProfile::LOG_FILE}")}
  131. end

  132. at_exit{
  133.   MemoryProfile::simple_count;
  134.   MemoryProfile::report;
  135. }
复制代码

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
2 [报告]
发表于 2013-07-05 17:45 |只看该作者

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
3 [报告]
发表于 2013-07-05 17:45 |只看该作者

论坛徽章:
5
丑牛
日期:2014-01-21 08:26:26卯兔
日期:2014-03-11 06:37:43天秤座
日期:2014-03-25 08:52:52寅虎
日期:2014-04-19 11:39:48午马
日期:2014-08-06 03:56:58
4 [报告]
发表于 2013-07-08 15:24 |只看该作者
相当好的memory profile
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP