Created
October 14, 2014 14:30
-
-
Save 2called-chaos/5ddea3add41fc827b316 to your computer and use it in GitHub Desktop.
Simple class to find memory eaters in Ruby / Rails applications
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
# put this in an initializer | |
# and use/output MemProfiler.profile all over the place (we use HTML comments). | |
class MemProfiler | |
def self.profile | |
Thread.main[:memprof_last_tick] ||= 0 | |
Thread.main[:memprof_last_tick] += 1 | |
was = Thread.main[:memprof_last_val] | |
now = Thread.main[:memprof_last_val] = measure | |
if was | |
"(#{Thread.main[:memprof_last_tick]}) #{Time.current.to_f}: PID #{$$} | was #{was} | is #{now} | diff #{now - was} | TID #{Thread.current.object_id}" | |
else | |
"(#{Thread.main[:memprof_last_tick]}) #{Time.current.to_f}: PID #{$$} | was unknown | is #{now} | diff #{now} | TID #{Thread.current.object_id}" | |
end | |
end | |
def self.measure | |
`ps ax -o pid,rss | grep -E "^[[:space:]]*#{$$}"`.strip.split.map(&:to_i)[1].to_i | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment