From 5777423b05cb6af1cbb36344aee280139c023910 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 17 Feb 2014 10:04:12 +1100 Subject: [PATCH] diff heaps used for memory leak detection --- script/diff_heaps.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 script/diff_heaps.rb diff --git a/script/diff_heaps.rb b/script/diff_heaps.rb new file mode 100644 index 00000000000..e854f2f65ae --- /dev/null +++ b/script/diff_heaps.rb @@ -0,0 +1,40 @@ +# to be used to compare ruby heaps generated in 2.1 +# can isolate memory leaks +# +# rbtrace -p 15193 -e 'Thread.new{require "objspace"; ObjectSpace.trace_object_allocations_start; GC.start(full_mark: true); ObjectSpace.dump_all(output: File.open("heap.json","w"))}.join' +# +# +require 'set' +require 'json' + +if ARGV.length != 2 + puts "Usage: diff_heaps [ORIG.json] [AFTER.json]" + exit 1 +end + +origs = Set.new + +File.open(ARGV[0], "r").each_line do |line| + parsed = JSON.parse(line) + origs << parsed["address"] if parsed && parsed["address"] +end + +diff = [] + +File.open(ARGV[1], "r").each_line do |line| + parsed = JSON.parse(line) + if parsed && parsed["address"] + diff << parsed unless origs.include? parsed["address"] + end +end + +diff.group_by do |x| + [x["type"], x["file"], x["line"]] +end.map {|x,y| + [x, y.count] +}.sort{ |a,b| + b[1] <=> a[1] +}.each{ |x,y| + puts "Leaked #{y} #{x[0]} objects at: #{x[1]}:#{x[2]}" +} +