Monitoring Ram Usage: Linux Commands And Techniques

how to monitor ram usage in linux

Memory management is a critical aspect of maintaining a Linux system's smooth operation. It is important to monitor RAM usage in Linux to ensure efficient resource allocation, prevent performance issues, and troubleshoot system bottlenecks. There are several commands and tools available to check memory usage in Linux, both through the command line and graphical user interfaces.

Some of the commonly used command-line tools to monitor RAM usage in Linux include:

- `free`: Displays information about total, used, and free memory, including RAM and swap space.

- `vmstat`: Reports virtual memory statistics, such as memory usage, paging, and CPU activity.

- `top`: Provides real-time information about CPU and memory usage on a per-process basis.

- `htop`: An enhanced version of `top` with a user-friendly interface, colour-coded output, and improved controls.

- `sar`: Monitors system performance, including memory utilisation, and can be used to track memory usage at regular intervals.

Additionally, the /proc/meminfo virtual file provides real-time data on system memory usage, and can be viewed using commands like `cat` or `less`.

For a graphical representation of memory usage, users can utilise the System Monitor app in Linux systems with a graphical desktop.

shundigital

Using the 'free' command

The `free` command is a simple way to monitor RAM usage in Linux. It summarises RAM and swap space usage, including total, used, free, shared, buff/cache, and available memory.

To use the `free` command, simply type `free` in your terminal. This will display information about memory and swap, expressed in kibibytes. You can also use the -m option to display memory usage in megabytes, or the -g option to display it in gigabytes.

Here's what each column in the output represents:

  • `total`: Total installed memory.
  • `used`: Memory currently in use (excluding buffers and cache).
  • `free`: Unused memory.
  • `shared`: Memory shared by multiple processes.
  • `buffers`: Memory reserved by the OS for allocating buffers when processes need them.
  • `cached`: Recently used files stored in RAM.
  • `buff/cache`: Buffers + Cache.
  • `available`: Estimation of how much memory is available for starting new applications, without swapping.

You can also use the `free` command with the `-/+ buffers/cache` option to see how much memory is available to applications. This will show the total used memory minus buffers and cache.

The `free` command has multiple options to customise the output:

  • Output in kibibytes, mebibytes, or gibibytes.
  • Detailed low and high memory statistics.
  • Old format (no `-/+ buffers/cache` line).
  • Total for RAM + swap.
  • Update every [delay] seconds.
  • Update [count] times.

For example, `free -m` will display memory usage in megabytes, and `free -g` will display it in gigabytes.

shundigital

Using the 'vmstat' command

The `vmstat` command is a built-in monitoring utility in Linux that reports virtual memory statistics. It provides a snapshot of the system's virtual memory statistics, including processes, memory, paging, block I/O, traps, and CPU activity. The syntax for the `vmstat` command is:

Vmstat [options] [delay [count]]

Here, `options` are switches to customize the output, `delay` defines the time interval between updates, and `count` is the number of updates printed after the given delay interval. If no count is set, the default is an infinite number of updates.

The basic output of the `vmstat` command displays system information in six sections:

  • Procs — provides the number of runnable processes (r) and the number of processes in uninterruptible sleep (b).
  • Memory — includes total virtual memory (swpd), total free memory (free), total memory used as buffers (buff), and total cache memory (cache).
  • Swap — shows the rate of swapping-in memory from disk (si) and the rate of swapping-out memory to disk (so).
  • IO — displays blocks received from a block device per second (bi) and blocks sent to a block device per second (bo).
  • System — includes the number of system interrupts (in) and the number of context switches per second (cs).
  • CPU — provides the percentage of CPU time spent on non-kernel processes (us), kernel processes (sy), idle time (id), waiting for Input/Output (wa), and stolen by a virtual machine (st).

To get a more detailed report, you can use the `-s` option, which displays various memory statistics, CPU and IO event counters, and slab statistics. This option provides a single-column output, which is easier to read than the default report.

Vmstat -s

The `-a` option replaces the buff and cache memory columns with inact and active columns, showing the amount of inactive and active memory in the system.

Vmstat -a

The `-f` option displays the number of forks since boot, which are new processes created from existing running ones.

Vmstat -f

The `-d` option gives read/write stats for various disks, providing three columns for each disk: Reads, Writes, and IO.

Vmstat -d

The `-t` option adds a timestamp to the report, showing when each update was generated.

Vmstat -t

By default, the `vmstat` command displays memory and swap statistics in kilobytes. To change the output units, use the `-S` option with the desired argument: `k` for kilobytes, `m` for megabytes, `K` for hexadecimal kilobytes, and `M` for hexadecimal megabytes.

Vmstat -S m

The `vmstat` command is a powerful tool for monitoring virtual memory statistics in Linux. It provides detailed information about system processes, memory usage, swap activity, input/output operations, CPU utilization, and more. By using various options and arguments, you can customize the output to suit your specific needs.

shundigital

Using the 'top' command

The top command is an essential tool for monitoring CPU and memory usage on a per-process basis. It provides a dynamic, real-time view of a running system, including the ability to check memory usage on a per-process basis. This is particularly important as you could have multiple iterations of the same command consuming different amounts of memory.

To launch the top command, simply enter 'top' in the terminal. The system will then display information about:

  • The number of running tasks
  • The number of logged-in users
  • CPU utilisation for each CPU or core
  • Memory and swap usage statistics for processes

The data is continuously updated, allowing you to follow the process activity and system resources in real time.

The top command also has an interactive interface that allows you to sort processes, change display settings, and manage operations without leaving the tool. For example, you can press Shift+m while running top to sort the programs by memory usage. This will let you see which processes are using the most memory at a glance. To exit the interface, simply press 'q'.

The top command is a great way to discover Process ID (PID) numbers of services that may be causing issues. With these PIDs, you can then troubleshoot or kill the offending tasks.

If you want to make the output of the top command more memory-focused, you can use the command 'top -o %MEM', which will sort all processes by memory used.

The top command is a versatile and powerful tool for monitoring memory usage in Linux. It provides a dynamic and interactive way to track memory usage on a per-process basis, making it easier to identify and manage memory-intensive processes.

shundigital

Using the 'htop' command

The htop command is a powerful tool for monitoring memory and CPU usage in Linux. It provides an interactive and user-friendly interface with colour-coded output, allowing users to view full command lines and scroll through processes vertically and horizontally.

To install htop, use the appropriate command for your Linux distribution:

  • For Ubuntu and Debian: sudo apt install htop
  • For RHEL 8+ and CentOS Stream: sudo dnf install htop
  • For Fedora: sudo dnf install htop
  • For openSUSE: sudo zypper install htop

Once installed, simply type htop in the terminal to run it. The output is divided into two main sections:

  • The top segment provides a summary, including graphic meters and text counters.
  • The lower section contains detailed data per process, allowing users to perform actions on individual processes.

The shortcuts at the bottom of the screen enable users to manipulate and customise processes without typing commands. To sort processes by memory usage, press F6 and then select %MEM using the arrow keys. To exit htop, press q.

The htop command provides an overview of the system's memory utilisation in several columns:

  • Kbmemfree: The amount of free memory in kilobytes.
  • Kbmemused: The amount of used memory in kilobytes.
  • %memused: The percentage of used memory.

To monitor memory usage over time, htop can be used in conjunction with the watch command. For example, to update memory usage every 5 seconds, the command would be: watch -n 5 htop.

The htop command is a valuable tool for system administrators, providing a clear and interactive way to monitor memory and CPU usage, as well as perform actions on individual processes.

shundigital

Using the 'ps' command

The `ps` command in Linux can tell us all sorts of information about the running processes on our system. The command also reports memory usage for each running process.

The default output of a `ps` command is sorted by the process number by default. However, this default behaviour can be changed with the use of the `--sort` or `k` options.

To sort by RAM percent usage, highest values first:

Bash

Ps aux --sort=-%mem

Or

Bash

Ps auxk-%mem

The `RSS` and `VSZ` columns also report RAM usage, though it's not as straightforward as the `%MEM` column.

The syntax is the same as above. Just specify which column you’d like to sort by.

Sort by `RSS` usage, highest values first:

Bash

Ps aux --sort=-rss

Or

Bash

Ps auxk-rss

Sort by `RSS` usage, highest values last:

Bash

Ps aux --sort=+rss

Or

Bash

Ps auxk+rss

Please note that the `+` can be omitted as it is a default option and thus makes `ps auxkrss` and `ps auxk+rss` identical.

The `ps` command is not an accurate tool to use for this intent. It is not reporting the real memory usage of processes. What it is really doing is showing how much real memory each process would take up if it were the only process running.

There are many other ways to monitor RAM usage on Linux.

Frequently asked questions

You can use the free command to check memory usage in Linux. This command displays the total amount of free and used memory in your system.

The "free" memory is the memory that is not currently being used by any processes. The "used" memory includes memory used by running processes, buffers, and cache.

You can use the top command to see a real-time view of memory and CPU usage by process. This command also allows you to sort processes by memory usage and provides interactive features for process management.

You can use the ps command to view memory consumption by a specific user or process. For example, the command ps -u -o pid,%mem will show the process ID (PID) and the percentage of memory consumed by that process.

Written by
Reviewed by
Share this post
Print
Did this article help you?

Leave a comment