Introduction
System administrators must ensure that a Linux system always operates at optimal levels. You can use a few simple terminal commands to access all relevant information and monitor memory statistics.
Find out how to check memory usage in Linux and accurately interpret the results.
Prerequisites
- Access to a command line/terminal.
- User with sudo privileges.
Note: If you are new to memory management, check out our comprehensive guide to memory management in operating systems. The guide covers basic memory management concepts and terminology which are needed to understand the contents of this guide.
Check Memory Usage in Linux via Command Line
Checking Linux memory usage via the command line is a highly efficient method and often the only way to check memory usage on remote headless servers.
Use the examples below to check memory usage via CLI.
cat Command
Use the cat command to open the /proc/meminfo file in your terminal:
cat /proc/meminfo
/proc/meminfo is a virtual file that reports the amount of available and used memory. It contains real-time information about the system's memory usage as well as buffers and shared memory used by the kernel.
This is an example of the /proc/meminfo file in Ubuntu 22.04:
The values in the /proc/meminfo file are expressed in kibibytes.
free Command
The free command is used to monitor resource usage. It summarizes RAM and swap space usage, including total, used, free, shared, buff/cache, and available memory.
Type free
in your terminal, without options, to display information about memory and swap, expressed in kibibytes:
free
Here's what each column represents:
Column | Description |
---|---|
total | Total installed memory. |
used | Memory currently in use (excluding buffers and cache). |
free | Unused memory. |
shared | Memory used by tmpfs and shared between processes. |
buff/cache | The sum of buffer and cache memory, which are used by the operating system to speed up processes. |
available | An estimation of how much memory is available for starting new applications, without swapping. |
The /proc/meminfo file offers more detailed information but free
presents data in a more understandable format. The available
value is vital as it displays how much memory is still available for running new applications.
The free
command has multiple options that allow users to customize the output.
Option | Result |
---|---|
-b | Output in bytes. |
-k | Output in kibibytes. |
-m | Output in mebibytes. |
-g | Output in gibibytes. |
-l | Detailed low and high memory statistics. |
-o | Old format (no -/+buffers/cache line). |
-t | Total for RAM + swap. |
-s | Update every [delay] seconds. |
-c | Update [count] times. |
Use these options to adapt memory usage output to your needs.
vmstat Command
The vmstat command reports virtual memory statistics. Type vmstat
to get a snapshot of the system's virtual memory statistics, including processes, memory, paging, block IO, traps, and CPU activity:
vmstat
Use the following list to interpret the virtual memory statistics provided by vmstat
.
Procs
- r - Number of processes waiting for run time.
- b - Number of processes in uninterruptible sleep.
Memory
- swpd - Amount of virtual memory used.
- free - Amount of idle memory.
- buff - Amount of memory used as buffers.
- cache - Amount of memory used as cache.
Swap
- si - Memory swapped in from disk (/s).
- so - Memory swapped to disk (/s).
IO
- bi - Blocks received from a block device (blocks/s).
- bo - Blocks sent to a block device (blocks/s).
System
- in - Number of interrupts per second, including the clock.
- cs - Number of context switches per second.
CPU
These values represent the percentage of total CPU time.
- us - Time spent running non-kernel code (user time, including nice time).
- sy - Time spent running kernel code (system time).
- id - Time spent idle. Before Linux 2.5.41, this includes IO-wait time.
- wa - Time spent waiting for IO. Before Linux 2.5.41, included in idle.
- st - Time stolen from a virtual machine. Before Linux 2.6.11, unknown.
Use the information from the fields to identify potential bottlenecks or areas that may require optimization.
top Command
The top command is essential for monitoring CPU and memory usage on a per-process basis. To launch it, enter top
in the terminal :
top
The system displays information about:
- System uptime.
- Average load.
- Number of running tasks.
- Number of logged-in users.
- CPU utilization for each CPU or core.
- Memory and swap usage statistics for processes.
The data is continuously updated, which allows you to follow the process activity and system resources in real time.
Note: Learn alternative methods and commands to check system uptime in our uptime command guide.
In addition to providing essential memory information, the top
command has an interactive interface that allows you to sort processes, change display settings, and manage operations without leaving the tool.
Press q
to exit the interface.
htop Command
Like the top
command, htop
provides memory and CPU usage monitoring data but with several enhancements for a more user-friendly environment and improved controls.
It features color-coded output and allows full command lines for processes to be viewed. Additionally, htop
supports vertical and horizontal scrolling for process lists.
To start htop
, enter:
htop
The output is divided into two main sections:
1. The top segment provides the summary and contains graphic meters and text counters.
2. The lower section structures the detailed data per process. This allows users to perform actions on individual processes.
The shortcuts at the bottom of the screen enable users to manipulate and customize the processes without typing commands.
If you encounter a Command htop not found message, htop
may not be installed on your system. To install htop
use the appropriate command for your Linux distribution:
Linux Distribution | Command |
---|---|
Debian/Ubuntu | sudo apt install htop |
RHEL 8+ and CentOS Stream | sudo dnf install htop |
Fedora | sudo dnf install htop |
Arch Linux | sudo pacman -S htop |
openSUSE | sudo zypper install htop |
Due to its many interactive capabilities, htop
is an essential system monitoring and process management tool.
sar Command
The sar
tool is a utility for monitoring system performance. It is not limited strictly to memory usage, but you can use the -r
option to track memory utilization statistics at regular intervals.
For example, enter the following command to direct sar
to check memory usage statistics every second, five times in a row:
sar -r 1 5
The metrics provide a detailed overview of your system's memory utilization in several columns, including:
- kbmemfree - The amount of free memory in kilobytes.
- kbmemused - The amount of used memory in kilobytes.
- %memused - The percentage of used memory.
The sar
utility is part of the sysstat
package, which may not be pre-installed on some Linux distributions. Use the appropriate command to install the sysstat
package:
Linux Distribution | Command |
---|---|
Debian/Ubuntu | sudo apt install sysstat |
RHEL 8+ and CentOS Stream | sudo dnf install sysstat |
Fedora | sudo dnf install sysstat |
Arch Linux | sudo pacman -S sysstat |
openSUSE | sudo zypper install sysstat |
Note: The man command provides a comprehensive overview of command functionalities. To explore options, flags, examples, and detailed information about the sar
command, enter the man sar
command in your terminal.
/proc/meminfo
The /proc/meminfo file is read-only and provides real-time data on system memory usage. Although commands like free
and cat
read this file to display memory usage statistics, it contains lots of information that may not be relevant for users who want a quick overview of their system's memory status.
Users looking for specific memory usage data can parse the /proc/meminfo file using tools like awk or grep. For example, the following command retrieves the total amount of free memory from the /proc/meminfo file:
grep MemFree /proc/meminfo
Key metrics from the /proc/meminfo file include:
- MemTotal. Total usable memory (RAM).
- MemFree. The amount of physical memory not being used by the system.
- MemAvailable. An estimate of the memory available for starting new applications without swapping.
- Buffers. Amount of physical memory used for file buffers.
- Cached. Amount of physical RAM used as cache memory.
- SwapTotal. Total amount of physical swap memory.
- SwapFree. Total amount of swap memory free. Memory evicted from RAM and temporarily on the disk.
- Active. Total amount of buffer/page cache memory that is in active use. This memory was recently used and is usually not reclaimed for other purposes.
- Inactive. Total amount of buffer/page cache memory that is free and available. This memory was not recently used and can be reclaimed for other purposes.
- Dirty. Memory waiting to be written back to the disk.
- Shmem. Total used shared memory (shared between several processes, thus including RAM disks, SYS-V-IPC, and BSD like SHMEM)
- SReclaimable: Part of the Slab cache that can be reclaimed if necessary (such as caches).
- AnonPages: Non-file backed pages mapped into userspace page tables.
- CommitLimit: Based on the overcommit ratio (vm.overcommit_ratio), this is the total amount of memory currently available to be allocated to the system. This limit is only adhered to if strict overcommit accounting is enabled (mode 2 in vm.overcommit_memory).
- Committed_AS: Amount of memory presently allocated on the system. The committed memory is a sum of all the memory allocated by processes, even if they have not yet used it.
The /proc/meminfo file contains many more values that may be relevant for system monitoring and troubleshooting tasks. You can use tools like grep
or awk
to focus your search on specific metrics.
Check Memory Usage in Linux via GUI
Using a graphical interface for server administration, especially in production environments, is less common. However, in development and testing environments, visual representations can make certain data sets, like memory usage, much clearer and easier to understand.
On Linux systems with a graphical desktop, you can access the System Monitor app to view memory consumption:
1. Navigate to Show Applications.
2. Enter System Monitor in the search bar and access the application.
3. Select the Resources tab.
The application displays a graphical overview of your memory consumption in real time, including historical information.
Conclusion
By following the examples in this article, you now know how to check memory usage on a Linux system and interpret the information correctly.
If your system frequently performs memory-intensive tasks, learn how to leverage swap memory to enhance stability and add additional virtual memory space.