Skip to content

Instantly share code, notes, and snippets.

@syntaxTerr0r
Last active February 10, 2025 13:52
Show Gist options
  • Save syntaxTerr0r/cc31678c8c4c09a0a7e35894ed600cfd to your computer and use it in GitHub Desktop.
Save syntaxTerr0r/cc31678c8c4c09a0a7e35894ed600cfd to your computer and use it in GitHub Desktop.
hunt for memory leak

ENABLE_MEMORY_DUMPS=true DUMP_PID1=87 DUMP_PID2=75 ATTEMPT=1

ps -eo pid,ppid,comm,rss,vsz --sort -rss | grep ruby

bundle exec rbtrace --pid=${DUMP_PID1} --eval="Thread.new{require 'objspace';ObjectSpace.trace_object_allocations_start}.join" bundle exec rbtrace --pid=${DUMP_PID2} --eval="Thread.new{require 'objspace';ObjectSpace.trace_object_allocations_start}.join"

bundle exec rbtrace --pid=${DUMP_PID1} --eval="Thread.new{require 'objspace'; GC.start(); io=File.open('/tmp/heap-${DUMP_PID1}-${ATTEMPT}.json', 'w'); ObjectSpace.dump_all(output: io); io.close}.join" --timeout=600 bundle exec rbtrace --pid=${DUMP_PID2} --eval="Thread.new{require 'objspace'; GC.start(); io=File.open('/tmp/heap-${DUMP_PID2}-${ATTEMPT}.json', 'w'); ObjectSpace.dump_all(output: io); io.close}.join" --timeout=600

gzip "/tmp/heap-${DUMP_PID1}-${ATTEMPT}.json" gzip "/tmp/heap-${DUMP_PID2}-${ATTEMPT}.json"

bundle exec rbtrace --pid=${DUMP_PID1} --eval="Thread.new{GC.start;require 'objspace';ObjectSpace.trace_object_allocations_stop;ObjectSpace.trace_object_allocations_clear}.join" bundle exec rbtrace --pid=${DUMP_PID2} --eval="Thread.new{GC.start;require 'objspace';ObjectSpace.trace_object_allocations_stop;ObjectSpace.trace_object_allocations_clear}.join"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment