How to Check Memory Usage in Linux via CLI and GUI

March 28, 2024

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.

Commands to check memory usage statistics in Linux.

Prerequisites

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:

Using the cat command to access the /proc/meminfo file in Linux.

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
Using the free command to check memory usage in Linux.

Here's what each column represents:

ColumnDescription
totalTotal installed memory.
usedMemory currently in use (excluding buffers and cache).
freeUnused memory.
sharedMemory used by tmpfs and shared between processes.
buff/cacheThe sum of buffer and cache memory, which are used by the operating system to speed up processes.
availableAn 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.

OptionResult
-bOutput in bytes.
-kOutput in kibibytes.
-mOutput in mebibytes.
-gOutput in gibibytes.
-lDetailed low and high memory statistics.
-oOld format (no -/+buffers/cache line).
-tTotal for RAM + swap.
-sUpdate every [delay] seconds.
-cUpdate [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
Using vmstat to check memory usage in Linux.

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.
Using the top command to check memory usage in Linux.

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 htop command in Linux.

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 DistributionCommand
Debian/Ubuntusudo apt install htop
RHEL 8+ and CentOS Streamsudo dnf install htop
Fedorasudo dnf install htop
Arch Linuxsudo pacman -S htop
openSUSEsudo 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.
Using the sar command to check memory usage in Linux.

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 DistributionCommand
Debian/Ubuntusudo apt install sysstat
RHEL 8+ and CentOS Streamsudo dnf install sysstat
Fedorasudo dnf install sysstat
Arch Linuxsudo pacman -S sysstat
openSUSEsudo 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
Using grep to parse the /proc/meminfo file.

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.

Using System Resources app to check memory usage in Linux.

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.

Was this article helpful?
YesNo
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
How to Check Disk Space in Linux
April 13, 2020

This tutorial shows how to display disk usage from a command line in Linux. It is important to know how much disk space you have used up, as well as how free disk space you have left, when managing your system.
Read more
How to Check Kernel Version in Linux in Command Line
April 18, 2024

The Linux kernel is much like the central brain of the operating system. This explains how to check the Linux kernel version of your operating system.
Read more
Best Tools to Monitor Network Bandwidth on a Linux Server
May 4, 2019

Most of the command-line utilities are designed to be lightweight, so they do not inflate bandwidth reports. This guide provides an overview of the best Linux network monitoring tools.
Read more
7 Ways to Reduce Server Response Time
December 20, 2023

Reducing server response time t is one of the primary goals of website owners and SEO experts. The first couple of seconds determine whether a client stays on your page or bounces on to the next one.
Read more