initial commit

This commit is contained in:
Loic Nageleisen 2015-07-31 12:14:37 +02:00
commit 9897ae9e22
8 changed files with 120 additions and 0 deletions

23
lib/meminfo.rb Normal file
View file

@ -0,0 +1,23 @@
require 'objspace'
require 'meminfo/ext'
# MemInfo gives a handful of methods allowing inspection of process memory use.
module MemInfo
module_function
class ProcessNotFound < StandardError; end
def rss(pid = Process.pid)
Integer(ps_rss(pid)) * 1024
rescue TypeError
raise ProcessNotFound, pid
end
def memsize
ObjectSpace.each_object.reduce(0) { |a, e| a + ObjectSpace.memsize_of(e) }
end
private def ps_rss(pid)
`ps ax -o rss,pid`.each_line.grep(/^\s*(\d+)\s+#{pid}$/) { $1 }.first
end
end