How can I see the memory usage of a Linux process?

how can I see the memory usage of a Linux process?


Solution 1:

You might want to have a look at 'pmap', e.g.

pmap -d [PROCESS ID]

At the bottom you will see how much memory is shared and how much of it is private to the process. For example: Some applications look like they use an awful lot of memory, but in reality it is just a lot of shared libraries that are taken into the calculation.

Solution 2:

On Linux: top: press [i] to enable column sorting. [<] or [>] to move the sort columns. Note the not-so-secret color mode by pressing [z] [x] [y] [X] [Y] change sort highlighting/color style.

free, ps auv shows swap too, sar (sysstat/psacct) are great for trending. slabtop, vmstat for the kernel. In general, watch is quick and easy for monitoring arbitrary commands but wastes processes watch -n0.3 'ps auv | grep top' watch -n5 'lsof | grep top | grep -v grep | wc -l'. A quick, reusable script is often a better way to go.

To really dig in when compiling from source, tcmalloc from google perftools.

For ongoing trending, have a look at collectd and cacti.

On Mac:

htop vs. top

  • Resident Set Size: 1530 1770 (error <10)
  • open files: 8 9

Average of 10 samples taken sequently after an hour. If I'm missing something, which I prol am, let me know.

Solution 3:

ps -o vsz,rss,%mem -p $PROCESS_ID

This will show the amount of memory the program has requested (virtual set size), how much is currently stored in RAM (resident set size), and what percentage that is of your total RAM.

For an explanation of the first two numbers that includes examples of analyzing real program behavior, read this atop case study analyzing memory leakage. If that's too much, at least read the "introduction to virtual memory" section.

Calculating the resident set size of a program is complicated by shared memory. Devin has a great explanation how shared memory can be accounted for using pmap to determine the marginal cost of starting another process.

Solution 4:

type "top" into your command line