Created
October 7, 2021 14:01
-
-
Save jturel/197a1c9b41004f8430ab10cf73c41acb to your computer and use it in GitHub Desktop.
memory profiler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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