内存检测数据分析工具:
https://github.com/tenderlove/heap-analyzer
http://tenderlove.github.io/heap-analyzer/
参考: https://blog.skylight.io/hunting-for-leaks-in-ruby/
(如果是proudction上使用rbtrace操作,不影响用户使用)
数据获取
- Gemfile
# 不指定用master的会遇到命令无法执行的问题,所以指定master分支
gem 'rbtrace', git: 'https://github.com/tmm1/rbtrace', branch: 'master'
- 例如:某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
- /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
- 分三个时段分别生成一个heap.json文件(触发第二步index内的代码执行)
数据分析
- 在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}"
网友评论