Linux process memory usage
Last update
2025-01-28
2025-01-28
«smem»
- install the C version of
smem
(no multiple python dependency!):
1 | apt install smemstat |
- simple wrapper to shows only top lines and support regexp filtering
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #!/usr/bin/env ruby if ARGV.include?('-h') puts "USAGE: #{File.basename __FILE__} [-a] [regexp]" exit end h, w = `stty size`.split(' ').map(&:to_i) lines = `sudo smemstat -d -m`.split("\n") lines.pop # Note: Memory reported in units of megabytes. lines.pop # empty lines totals = lines.pop # Totals header = lines.shift cmd_col = header.index('Command') if ARGV.include?('-a') ARGV.delete '-a' h = 100_000 end # replace Command with full cmdline lines = lines.map{|l| pid = l.split(' ', 2).first.to_i next if pid == Process.pid cmd_src = l[cmd_col..] cmd_dst = File.read("/proc/#{pid}/cmdline") rescue cmd_src args = cmd_dst.split(/[ \u0000]/) cmd_dst = [File.basename(args.shift)].concat(args).join(' ') "#{l[0...cmd_col]}#{cmd_dst}" }.compact lines = lines.grep Regexp.new(ARGV[0]) if ARGV[0] lines = lines[0..(h-9)] max_w = lines.map(&:size).max sep = '-' * (max_w > w ? (w-2) : max_w) puts header puts sep lines.each{|l| puts l[0..(w-2)] } puts sep puts totals |
Source: golinuxcloud, smem (python), smemstat (C)