Create a gist now

Instantly share code, notes, and snippets.

@thephw /OOM_helpers.rb Secret
Last active Apr 26, 2016

What would you like to do?

Dump Allocations

This creats a flat text file of the allocations dump from ruby prof

Dump Heap File

This dumps all of the tract object allocations from ObjectSpace

Memory Profile

Wrapper to output file with pretty print of https://github.com/SamSaffron/memory_profiler

module LeakHelpers
def self.dump_heap_file
require 'objspace'
ObjectSpace.trace_object_allocations_start
yield
ensure
io=File.open("./#{Time.now.strftime("%H.%M.%S")}_heap_dump", "w")
ObjectSpace.dump_all(output: io);
io.close
end
def self.dump_allocations
require 'ruby-prof'
RubyProf.measure_mode = RubyProf::ALLOCATIONS
result = RubyProf.profile do
yield
end
ensure
io=File.open("./#{Time.now.strftime("%H.%M.%S")}_allocations_dump", "w")
flat_printer = RubyProf::FlatPrinter.new(result)
flat_printer.print(STDOUT, :min_percent => 2)
io.close
end
def self.memory_profile
require 'memory_profiler'
report = MemoryProfiler.report do
yield
end
io=File.open("./#{Time.now.strftime("%H.%M.%S")}_memory_profile", "w")
io.puts report.pretty_print
io.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment