Skip to content

Instantly share code, notes, and snippets.

@scotchi
Created February 20, 2013 13:05
Show Gist options
  • Save scotchi/4995413 to your computer and use it in GitHub Desktop.
Save scotchi/4995413 to your computer and use it in GitHub Desktop.
Mini-tool to get the amount of memory being used by a process, *not* the amount of memory plus the size of mmaped files. In other words, it's a way of removing the mmaped size of PIDs.
#!/usr/bin/env ruby
PID = ARGV.shift
smaps ="/proc/#{PID}/smaps"
if PID.nil?
warn 'You must specify a PID'
exit 1
end
begin
kb = 0
from = nil
File.open("/proc/#{PID}/smaps").each do |line|
if line =~ /^[0-9a-f]+-[0-9a-f]+/i
from = line.chomp[73..-1] if line =~ /^[0-9a-f]+-[0-9a-f]+/i
end
kb += line.split(/\s+/)[1].to_i if line =~ /^Size:/ && (!from || from.include?('[heap]'))
end
puts "#{kb / 1024} MB"
rescue
warn "Couldn't open #{smaps}"
exit 2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment