57

Does anyone know of a tool to visually show the memory usage of a selected process on Ubuntu?

ps aux will show a numerical snapshot, but I'd really like a line I can watch change as I hammer the process and hopefully see unexpected behaviours.

Has anyone got any suggestions?

franzlorenzon
  • 5,845
  • 6
  • 36
  • 58
BanksySan
  • 27,362
  • 33
  • 117
  • 216

7 Answers7

128

I couldn't find any real tools to do it.

But I have found a neat small set of scripts that'll do it.

Using this little bash loop to do the logging:

while true; do
ps -C <ProgramName> -o pid=,%mem=,vsz= >> /tmp/mem.log
gnuplot /tmp/show_mem.plt
sleep 1
done &

This will create a nice little log file of memory usage called /tmp/mem.log. Then it generates an image of the data with gnuplot using the following script (put this in /tmp/show_mem.plt):

set term png small size 800,600
set output "mem-graph.png"

set ylabel "VSZ"
set y2label "%MEM"

set ytics nomirror
set y2tics nomirror in

set yrange [0:*]
set y2range [0:*]

plot "/tmp/mem.log" using 3 with lines axes x1y1 title "VSZ", \
     "/tmp/mem.log" using 2 with lines axes x1y2 title "%MEM"

Then opening the image with the default GNOME image viewer it keeps reloading the image when it changes. So if all the above loop is backgrounded it will appear that you have an amazing memory usage graphing tool running within an image viewer :)

The process I'm tracking right now looks like this: Graph of rising memory usage

It looks like I do have some memory issues :(

Much of this was ripped from http://brunogirin.blogspot.com.au/2010/09/memory-usage-graphs-with-ps-and-gnuplot.html, credit where it is due.

Atcold
  • 683
  • 2
  • 10
  • 31
LovesTha
  • 1,663
  • 1
  • 14
  • 17
  • I love your contribution man! Just a word of advise, in my machine 1s delay was too soon and cpu usage spiked to about 50%, changed it to 5s which was still good enough for me and all is great :) – Purefan Jun 03 '14 at 21:36
  • 4
    I have improved it a little bit to make it completely automated. My version is https://gist.github.com/nicolasazrak/32d68ed6c845a095f75f037ecc2f0436 – The SWE Jul 12 '17 at 15:48
22

The accepted answer worked for me, but I was a bit tired of doing so much copy/pasting every time I want to measure memory, so I've created a small tool for this: https://github.com/parikls/mem_usage_ui

Install using

pip install mem_usage_ui

Start the browser GUI by typing mem_usage_ui in the shell.

This is what the result will look like:

enter image description here

tink
  • 14,342
  • 4
  • 46
  • 50
parikLS
  • 463
  • 4
  • 13
17

Resident set size of the target, $PID, process can be streamed to ttyplot for live display:

while :; do grep -oP '^VmRSS:\s+\K\d+' /proc/$PID/status \
    | numfmt --from-unit Ki --to-unit Mi; sleep 1; done | ttyplot -u Mi

asciicast

ttyplot -u Mi above can be replaced with feedgnuplot --stream --line --point --xlen 20 to get more or less the same plot in GUI (of gnuplot, which, for instance, has image export). feedgnuplot should be installable with apt-get.

saaj
  • 23,253
  • 3
  • 104
  • 105
  • Good answer because answer for a live plot answer – bux May 20 '22 at 09:02
  • Only answer that shows usage over time in a tty! Nice. – Endareth Aug 25 '22 at 03:31
  • 1
    `while :; do grep -oP '^MemAvailable:\s+\K\d+' /proc/meminfo | numfmt --from-unit Ki --to-unit Mi; sleep 1; done | ttyplot -u Mi` to plot all available system memory instead – Jamie Pate Nov 11 '22 at 02:51
  • And how do I get the PID number of a process before I start it? Would I use the PID of the shell that I'm running? This isn't an end-to-end answer unfortunately for this reason. – Cornelius Roemer Dec 19 '22 at 17:34
  • E.g. try `pgrep`. Replace `$PID` with `$(pgrep --newest PROGRAM)`. – saaj Dec 21 '22 at 09:59
12

Python package Memory Profiler works with non-Python executable binaries too.

Use mprof to generate a full memory usage report of your executable and to plot it:

mprof run <executable>
mprof plot

The plot would be something like this:

Performance plot

It is available on PyPI, so it can be installed:

pip install -U memory_profiler
cartoonist
  • 5,991
  • 6
  • 37
  • 40
9

I really like using "htop" instead of "top". It's very colorful and has a lot of options like setup, search, invert, tree, sort by, nice, kill. Give it a try:

$ sudo apt-get install htop

htop

César
  • 9,939
  • 6
  • 53
  • 74
  • 6
    Cheers César, however I was hoping for a GUI interface... some pretty lines going up and down on a time v memory axis. I don't really want to have to write something to pull data out and graph it myself. Any ideas about that bit? – BanksySan Nov 07 '11 at 12:25
  • Most spreadsheet programs can import data in columns and then graph it. – DaveWalley Apr 10 '14 at 21:19
  • 3
    While I'm a big fan of htop, it's much better at displaying instantaneous memory usage rather than memory usage over time. – Nick Desaulniers Mar 08 '18 at 08:14
  • 2
    htop has a graph mode to display memory/CPU usage over time: https://hisham.hm/htop/index.php?page=screenshots – ybeltukov Mar 13 '18 at 12:55
4

Top will do the trick

top -b | grep {name of process}
top -b -p {PID}
top -b -u {userid}
b7kich
  • 4,253
  • 3
  • 28
  • 29
-3

Try running the command "top" in the command line. This will display a list of processes similar to the windows task manager.

NickLH
  • 2,643
  • 17
  • 28