Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple class to find memory eaters in Ruby / Rails applications
# 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
You can’t perform that action at this time.