SHOW:
|
|
- or go back to the newest paste.
1 | text = `ps -eo comm,rss` | |
2 | #split the input to two colums | |
3 | - | #split the input into two colums: [progname,memusage] |
3 | + | table=text.lines.collect {|l| |
4 | - | table=text.lines.collect {|l| [l.split[0..-2].join ,l.split[-1].to_i/1024]} |
4 | + | [l.split[0..-2].join ,l.split[-1].to_i/1024]} |
5 | ||
6 | - | # sort decrasing on the second field (memory usage) |
6 | + | #summing rows with same progname |
7 | - | table.sort!{|x,y| y[1]<=>x[1]} |
7 | + | sum=Hash.new(0) |
8 | table.each {|i| sum[i[0]]+=i[1]} | |
9 | ||
10 | # sort decrasing on second field (memory use) | |
11 | - | res+= i[0]+" ("+i[1].to_s+")"+" " |
11 | + | table=sum.to_a.sort{|x,y| y[1]<=>x[1]} |
12 | - | } |
12 | + | |
13 | #format and print output | |
14 | puts table[0..4].inject("Memory usage (MB):\n"){|res,i| | |
15 | res+= i[0]+" ("+i[1].to_s+") " | |
16 | }.chop |