Module: HeapScope::Fragmentation

Defined in:
lib/heapscope/dominators.rb

Overview

Heuristic fragmentation / native mismatch indicators.

Class Method Summary collapse

Class Method Details

.assess(snapshot) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/heapscope/dominators.rb', line 55

def assess(snapshot)
  gc = snapshot.gc_stat
  rss = snapshot.rss_bytes
  live = gc[:heap_live_slots]
  free = gc[:heap_free_slots]
  pages = gc[:heap_allocated_pages]
  return { status: :unknown, note: "Insufficient GC/RSS data." } if rss.nil? || live.nil?

  heap_bytes_estimate = live.to_i * 40 # very rough average slot estimate
  ratio = heap_bytes_estimate.positive? ? rss.to_f / heap_bytes_estimate : nil
  free_ratio = (live.to_i + free.to_i).positive? ? free.to_f / (live + free) : nil

  status =
    if ratio && ratio > 4.0 && pages.to_i > 100
      :potential_fragmentation
    elsif ratio && ratio > 2.5
      :elevated_rss_vs_heap
    else
      :normal
    end

  {
    status: status,
    rss_bytes: rss,
    heap_bytes_estimate: heap_bytes_estimate,
    rss_to_heap_ratio: ratio&.round(2),
    free_slot_ratio: free_ratio&.round(3),
    heap_allocated_pages: pages,
    note: "Potential allocator/heap fragmentation indicator — not confirmed fragmentation."
  }
end