How to check memory usage on Linux

Question: I would like to monitor memory usage on my Linux system. What are the available GUI-based or command-line tools for checking current memory usage of Linux?

When it comes to optimizing the performance of a Linux system, physical memory is the single most important factor. Naturally, Linux offers a wealth of options to monitor the usage of the precious memory resource. Different tools vary in terms of their monitoring granularity (e.g., system-wide, per-process, per-user), interface (e.g., GUI, command-line, ncurses) or running mode (e.g., interactive, batch mode).

Here is a non-exhaustive list of GUI or command-line tools to choose from to check used and free memory on Linux platform.

1. /proc/meminfo

The simpliest method to check RAM usage is via /proc/meminfo. This dynamically updated virtual file is actually the source of information displayed by many other memory related tools such as free, top and ps tools. From the amount of available/free physical memory to the amount of buffer waiting to be or being written back to disk, /proc/meminfo has everything you want to know about system memory usage. Process-specific memory information is also available from /proc/<pid>/statm and /proc/<pid>/status

$ cat /proc/meminfo

2. atop

The atop command is an ncurses-based interactive system and process monitor for terminal environments. It shows a dynamically-updated summary of system resources (CPU, memory, network, I/O, kernel), with colorized warnings in case of high system load. It also offers a top-like view of processes (or users) along with their resource usage, so that system admin can tell which processes or users are responsible for system load. Reported memory statistics include total/free memory, cached/buffer memory and committed virtual memory.

$ sudo atop

3. free

The free command is a quick and easy way to get an overview of memory usage gleaned from /proc/meminfo. It shows a snapshot of total/free physical memory and swap space of the system, as well as used/free buffer space in the kernel.

$ free -h

4. GNOME System Monitor

GNOME System Monitor is a GUI application that shows a short history of system resource utilization for CPU, memory, swap space and network. It also offers a process view of CPU and memory usage.

$ gnome-system-monitor

5. htop

The htop command is an ncurses-based interactive processor viewer which shows per-process memory usage in real time. It can report resident memory size (RSS), total program size in memory, library size, shared page size, and dirty page size for all running processes. You can scroll the (sorted) list of processes horizontally or vertically.

$ htop

6. KDE System Monitor

While GNOME desktop has GNOME System Monitor, KDE desktop has its own counterpart: KDE System Monitor. Its functionality is mostly similar to GNOME version, i.e., showing a real-time history of system resource usage, as well as a process list along with per-process CPU/memory consumption.

$ ksysguard

7. memstat

The memstat utility is useful to identify which executable(s), process(es) and shared libraries are consuming virtual memory. Given a process ID, memstat identifies how much virtual memory is used by the process' associated executable, data, and shared libraries.

$ memstat -p <PID>

8. nmon

The nmon utility is an ncurses-based system benchmark tool which can monitor CPU, memory, disk I/O, kernel, filesystem and network resources in interactive mode. As for memory usage, it can show information such as total/free memory, swap space, buffer/cached memory, virtual memory page in/out statistics, all in real time.

$ nmon

9. ps

The ps command can show per-process memory usage in real-time. Reported memory usage information includes %MEM (percent of physical memory used), VSZ (total amount of virtual memory used), and RSS (total amount of physical memory used). You can sort the process list by using "--sort" option. For example, to sort in the decreasing order of RSS:

$ ps aux --sort -rss

10. smem

The smem command allows you to measure physical memory usage by different processes and users based on information available from /proc. It utilizes proportional set size (PSS) metric to accurately quantify effective memory usage of Linux processes. Memory usage analysis can be exported to graphical charts such as bar and pie graphs.

$ sudo smem --pie name -c "pss"

11. top

The top command offers a real-time view of running processes, along with various process-specific resource usage statistics. Memory related information includes %MEM (memory utilization percentage), VIRT (total amount of virtual memory used), SWAP (amount of swapped-out virtual memory), CODE (amount of physical memory allocated for code execution), DATA (amount of physical memory allocated to non-executable data), RES (total amount of physical memory used; CODE+DATA), and SHR (amount of memory potentially shared with other processes). You can sort the process list based on memory usage or size.

12. vmstat

The vmstat command-line utility displays instantaneous and average statistics of various system activities covering CPU, memory, interrupts, and disk I/O. As for memory information, the command shows not only physical memory usage (e.g., tota/used memory and buffer/cache memory), but also virtual memory statistics (e.g., memory paged in/out, swapped in/out).

$ vmstat -s

Download this article as ad-free PDF (made possible by your kind donation): 
Download PDF

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

10 thoughts on “How to check memory usage on Linux

  1. Thanks for the detailed, all in one place article.
    Personally, I use smem for a quick look at the top processes consuming memory their swap usage, if any.
    For example.
    $ smem -tkr -s rss -r | awk 'NR < 11;{y=x "\n" $0; x=$0};END{print y}'

    Returns:

    PID User Command Swap USS PSS RSS
    23316 aram /usr/lib/firefox/firefox 24.2M 721.0M 722.6M 729.1M
    23395 aram chromium-browser --type=gpu 122.6M 215.5M 263.3M 314.4M
    23319 aram chromium-browser --disable- 31.5M 140.1M 166.7M 204.1M
    2433 aram cinnamon --replace 35.8M 141.1M 142.4M 148.4M
    23340 aram /opt/google/chrome-unstable 25.9M 89.7M 96.7M 112.4M
    23362 aram skype 84.1M 83.2M 87.3M 91.5M
    23753 aram /opt/google/chrome-unstable 68.7M 76.3M 81.5M 89.3M
    25244 aram pidgin 0 36.3M 41.6M 52.7M
    22946 aram /home/aram/.dropbox-dist/dr 37.7M 39.0M 39.1M 40.6M
    -------------------------------------------------------------------------------
    66 1 658.0M 1.6G 1.7G 2.0G

  2. Under Gnome: Gnome system monitor
    Under KDE: KSysGuard
    Under Xfce4: Systemload
    Under LXDE: one of those above
    Under enlightenment: e-monitor (at least in e-17, but I'm not sure for the newer versions)

  3. The "classic" variant: install package sysstat, and use: sar -r. In RHEL/Fedora sysstat collects monitoring data to /var/log/sa using a cron job. But sar can be using directly from command line too:
    sar -r
    e.g.
    sar -r 60 5
    5 periods a 1 minutes

  4. The author forgot to mention /proc/PID/maps (with PID a process number) that give detailed information about what is using the memory in a given process.

    For a more readable ouput, use 'pmap PID' or 'pmap -x PID' for advanced information:

    pmap -x 2255
    2255: xfce4-volumed
    Address Kbytes RSS Dirty Mode Mapping
    00007ff5d0000000 132 32 32 rw--- [ anon ]
    00007ff5d0021000 65404 0 0 ----- [ anon ]
    00007ff5d4559000 44 16 0 r-x-- libxfce.so
    00007ff5d4564000 2044 0 0 ----- libxfce.so
    00007ff5d4763000 4 4 4 r---- libxfce.so
    00007ff5d4764000 4 4 4 rw--- libxfce.so
    00007ff5d4765000 4 0 0 ----- [ anon ]
    ...

    By the way, I have question about that output. Each library appears 4 times:
    - The r-x-- line is for executable code
    - The r---- line is for read-only data (e.g. const)
    - The rw--- line is for writable data

    But what is the line ----- for?
    It is neither readable, writable nor executable and it can never be used.
    Its RSS column (when using -x) is always 0 which shows that it is indeed never used so it costs nothing.

  5. In HP OVPM there are ready charts like max %cpu, %memory utilization over time range. I wonder what data points they use ? I have found top to be most flexible and simple to use. The first few lines itself gives me cpu and mem utilization.

    Cpu us (user mode) and sy (system mode) time gives the data points for %cpu utilization.
    Mem will need some math to get % as used / total * 100.

    netstat -i | grep eth can help get transmitted data.
    iostat can help get disk io data.
    df can help with disk utilization.

    But would be good to know some mappings of what HP OVPM uses so I can avoid checking with sys ops group for data.

  6. I would suggest to include sysstat package too, because sar can display a real-time memory usage as well as extract historical data.

  7. I'm surprised nobody mentioned slab memory, often where many leaks can be spotted. Collectl not only monitors everything all the other tools look at, it can also show consumption by slabs by name. Both real time, historical and even across a cluster, via colmux.
    -mark

Leave a comment

Your email address will not be published. Required fields are marked *