Skip to content

Instantly share code, notes, and snippets.

@jturel
Created October 7, 2021 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jturel/197a1c9b41004f8430ab10cf73c41acb to your computer and use it in GitHub Desktop.
Save jturel/197a1c9b41004f8430ab10cf73c41acb to your computer and use it in GitHub Desktop.
memory profiler
class MemoryEndpoint
def call(env)
time_str = Time.now.strftime('%Y-%m-%d_%H-%M-%S')
File.open("/tmp/dump-#{Process.pid}-#{time_str}.json", 'w') do |file|
dump_memory(file)
end
[200, { 'Content-Type' => 'text/plain' }, ["Dumped Memory #{Process.pid}"]]
end
private
def dump_memory(file)
require 'objspace'
#ObjectSpace.trace_object_allocations_start
GC.start
ObjectSpace.dump_all(output: file)
end
end
class TraceEndpoint
def call(env)
require 'objspace'
ObjectSpace.trace_object_allocations_start
[200, { 'Content-Type' => 'text/plain' }, ["Tracing #{Process.pid}"]]
end
end
map ENV['RAILS_RELATIVE_URL_ROOT'] || '/' do
run Foreman::Application
end
map '/dump-memory' do
run MemoryEndpoint.new
end
map '/trace' do
run TraceEndpoint.new
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment