rails rbtrace定位内存泄露

内存检测数据分析工具:
https://github.com/tenderlove/heap-analyzer
http://tenderlove.github.io/heap-analyzer/

参考: https://blog.skylight.io/hunting-for-leaks-in-ruby/

(如果是proudction上使用rbtrace操作,不影响用户使用)

数据获取

  1. Gemfile
# 不指定用master的会遇到命令无法执行的问题,所以指定master分支

gem 'rbtrace', git: 'https://github.com/tmm1/rbtrace', branch: 'master'
  1. 例如:某controller.rb中
def index
  if params[:rbtrace] = "go"
    require 'rbtrace'
    pid = Process.pid
    system("nohup bundle exec rbtrace -p #{pid} -e 'load \"/tmp/heap_dump.rb\"' &")
  end
end
  1. /tmp/heap_dump.rb文件
Thread.new do
  require "objspace"
  ObjectSpace.trace_object_allocations_start
  GC.start
  ObjectSpace.dump_all(output: File.open("/tmp/heap.json", "w"))
end.join
  1. 分三个时段分别生成一个heap.json文件(触发第二步index内的代码执行)

数据分析

  1. 在bundle exec rails console内执行分析的代码输出到log文件(或者你可以写个脚本执行单独保存或者使用文章开始使用的数据分析工具)
first_addrs = []
third_addrs = []

# Get a list of memory addresses from the first dump
File.open("/tmp/heap1.json", "r").each_line do |line|
parsed = JSON.parse(line)
first_addrs << parsed["address"] if parsed && parsed["address"]
end

# Get a list of memory addresses from the last dump
File.open("/tmp/heap3.json", "r").each_line do |line|
parsed = JSON.parse(line)
third_addrs << parsed["address"] if parsed && parsed["address"]
end

diff = []

# Get a list of all items present in both the second and
# third dumps but not in the first.
File.open("/tmp/heap2.json", "r").each_line do |line|
  parsed = JSON.parse(line)
  if parsed && parsed["address"] && !first_addrs.include?(parsed["address"]) && third_addrs.include?(parsed["address"])
  puts diff.count
  puts parsed
    diff << parsed
  end
end


# Group items

diff.group_by do |x|

[x["type"], x["file"], x["line"]]

end.map do |x,y|

# Collect memory size

[x, y.count, y.inject(0){|sum,i| sum + (i['bytesize'] || 0) }, y.inject(0){|sum,i| sum + (i['memsize'] || 0) }]

end.sort do |a,b|

b[1] <=> a[1]

end.each do |x,y,bytesize,memsize|

# Output information about each potential leak

Rails.logger.info "Leaked #{y} #{x[0]} objects of size #{bytesize}/#{memsize} at: #{x[1]}:#{x[2]}"

end


# Also output total memory usage, because why not?
memsize = diff.inject(0){|sum,i| sum + (i['memsize'] || 0) }
bytesize = diff.inject(0){|sum,i| sum + (i['bytesize'] || 0) }
puts "\n\nTotal Size: #{bytesize}/#{memsize}"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值