Skip to content

Instantly share code, notes, and snippets.

@2called-chaos
Created October 14, 2014 14:30
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 2called-chaos/5ddea3add41fc827b316 to your computer and use it in GitHub Desktop.
Save 2called-chaos/5ddea3add41fc827b316 to your computer and use it in GitHub Desktop.
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