Skip to content

Commit

Permalink
DEV: improve diagnostics on mem leak checker
Browse files Browse the repository at this point in the history
This adds mwrap logging to each iteration so we can see how much
leaks per iteration and where it is coming from
  • Loading branch information
SamSaffron committed Oct 3, 2019
1 parent df02930 commit 1d5c2b3
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions script/test_memory_leak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,69 @@ def iter(warmup: false)
end
end

def render_table(array)
buffer = +""

width = array[0].map { |k| k.to_s.length }
cols = array[0].length

array.each do |row|
row.each_with_index do |val, i|
width[i] = [width[i].to_i, val.to_s.length].max
end
end

array[0].each_with_index do |col, i|
buffer << col.to_s.ljust(width[i], ' ')
if i == cols - 1
buffer << "\n"
else
buffer << ' | '
end
end

buffer << ("-" * (width.sum + width.length))
buffer << "\n"

array.drop(1).each do |row|
row.each_with_index do |val, i|
buffer << val.to_s.ljust(width[i], ' ')
if i == cols - 1
buffer << "\n"
else
buffer << ' | '
end
end
end

buffer
end

def mwrap_log
report = +""

Mwrap.quiet do
report << "Allocated bytes: #{Mwrap.total_bytes_allocated} Freed bytes: #{Mwrap.total_bytes_freed}\n"
report << "\n"

table = []
Mwrap.each(200000) do |loc, total, allocations, frees, age_sum, max_life|
table << [total, allocations - frees, frees == 0 ? -1 : (age_sum / frees.to_f).round(2), max_life, loc]
end

table.sort! { |a, b| b[1] <=> a[1] }
table = table[0..50]

table.prepend(["total", "delta", "mean_life", "max_life", "location"])

report << render_table(table)
end

report
end

Mwrap.clear

if $mwrap
$mwrap_baseline = Mwrap.total_bytes_allocated - Mwrap.total_bytes_freed
end
Expand All @@ -105,4 +168,8 @@ def iter(warmup: false)

100000.times do
iter
if $mwrap
puts mwrap_log
GC.start(full_mark: true, immediate_sweep: true)
end
end

0 comments on commit 1d5c2b3

Please sign in to comment.