How To Check Memory Usage In Linux From CLI and GUI

Updated May 15, 2024

Monitoring memory usage is an important task for Linux system administrators and users who want to improve their system's performance. Linux has several command line tools that show real-time information about memory use, letting you track and study memory usage. In this article, we'll look at some of the most helpful command line tools for checking memory usage on Linux systems, including cat, free, vmstat, top, htop, sar, and how to understand the /proc/meminfo file.

Key Takeaways

  • The cat /proc/meminfo command shows real-time memory metrics, such as total usable RAM, free memory, and cached memory.
  • The free command provides a user-friendly output of memory usage statistics, including total, used, free, shared, buff/cache, and available memory.
  • The vmstat command reports a snapshot of the system's memory usage, including processes, memory, paging, block IO, traps, and CPU activity.
  • The top command is an interactive tool that shows real-time information about system resource utilization, including CPU and memory usage on a per-process basis.
  • The htop command is an improved version of top, offering a more user-friendly interface and extra features like color-coding and process management using shortcuts.

Using Command Line Tools to Check Memory Usage

Linux has several command line tools that let you check memory usage in real-time. These tools are important for system administrators and users who want to monitor and optimize their system's performance.

cat Command

The cat command is a simple way to view the contents of the /proc/meminfo file, which has real-time information about the system's memory usage.

cat /proc/meminfo

This command will show various memory metrics, such as:

  • MemTotal: Total usable RAM
  • MemFree: The amount of physical RAM left unused
  • MemAvailable: An estimate of how much memory is available for starting new applications, without swapping
  • Buffers: The amount of temporary storage for raw disk blocks
  • Cached: The amount of physical RAM used as cache memory

Here's an example output:

MemTotal:        8116552 kB
MemFree:          385440 kB
MemAvailable:    1890092 kB
Buffers:          259300 kB
Cached:          1345384 kB
...

free Command

The free command provides a more user-friendly output of the memory usage statistics. It shows the total, used, free, shared, buff/cache, and available memory.

free -h

The -h option shows the output in a human-readable format. Here's an example output:

              total        used        free      shared  buff/cache   available
Mem:          7.8Gi       6.1Gi       387Mi       1.0Gi       1.3Gi       1.8Gi
Swap:         2.0Gi       1.1Gi       912Mi

You can also use other options like -b (bytes), -k (kibibytes), -m (mebibytes), or -g (gibibytes) to change the output units.

vmstat Command

The vmstat command reports virtual memory statistics and other system information. It provides a snapshot of the system's memory usage, including processes, memory, paging, block IO, traps, and CPU activity.

vmstat

Here's an example output:

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0 1132052 395700 259300 1345384    0    1    33    52   85  176  9  5 85  1  0

The memory section shows the amount of swap space used (swpd), free memory (free), buffer memory (buff), and cache memory (cache).

top Command

The top command is an interactive tool that shows real-time information about system resource utilization, including CPU and memory usage on a per-process basis.

top

Here's a simplified example output:

top - 10:30:00 up 2 days, 18:00,  1 user,  load average: 0.50, 0.68, 0.83
Tasks: 287 total,   1 running, 286 sleeping,   0 stopped,   0 zombie
%Cpu(s):  8.7 us,  5.0 sy,  0.0 ni, 85.3 id,  1.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7854.2 total,    386.4 free,   6119.4 used,   1348.4 buff/cache
MiB Swap:   2048.0 total,    938.2 free,   1109.8 used.   1711.6 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
   1234 user      20   0 1234.5m 123.4m  12.3m S   5.0   1.6   1:23.45 process_name

The top command provides a dynamic view of the system's processes and their resource consumption. You can sort processes by memory usage using the M key and manage processes using various interactive commands.

htop Command

The htop command is an improved version of the top command, offering a more user-friendly interface and extra features.

htop

The htop command shows a color-coded, real-time view of the system's processes and resource usage. It supports vertical and horizontal scrolling, process searching, and process management using shortcuts.

sar Command

The sar command is a system activity reporter that monitors various aspects of system performance, including memory usage.

sar -r 1 5

This command will show memory utilization statistics every 1 second for 5 iterations. Here's an example output:

07:30:01 PM kbmemfree kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty
07:30:02 PM    385600   6127104     94.07    259300   1345384  16327220    130.59   5132848   1347188         0
07:30:03 PM    385724   6126980     94.07    259300   1345380  16327220    130.59   5134528   1347168         0
07:30:04 PM    385620   6127084     94.07    259300   1345384  16327220    130.59   5132784   1347188         0
07:30:05 PM    385752   6126952     94.07    259300   1345380  16327220    130.59   5134336   1346992         0
07:30:06 PM    385600   6127104     94.07    259300   1345384  16327220    130.59   5132624   1347184         0
Average:       385659   6127045     94.07    259300   1345382  16327220    130.59   5133424   1347144         0

The output includes metrics like kbmemfree (free memory in kilobytes), kbmemused (used memory in kilobytes), and %memused (percentage of used memory).

/proc/meminfo File

The /proc/meminfo file is a virtual file that provides real-time information about the system's memory usage. You can parse this file using tools like awk or grep to get specific metrics.

The /proc/meminfo file has various memory metrics, such as MemTotal, MemFree, Buffers, Cached, Active, and Inactive. These metrics provide a detailed overview of the system's memory usage.

Here's an example of using awk to get specific metrics:

awk '/MemTotal/{print $2}' /proc/meminfo

This command will print the value of the MemTotal metric in kilobytes.

These command line tools offer a powerful and flexible way to monitor memory usage on Linux systems. By learning these tools, system administrators and users can track and optimize their system's memory utilization.

Using GUI Tools to Check Memory Usage

While command line tools are powerful and flexible, Linux also offers graphical tools for monitoring memory usage. These tools are useful for users who prefer a visual interface or are less comfortable with the command line.

System Monitor App

The System Monitor app is a graphical application that provides a real-time overview of system resource usage, including memory consumption. It is available on most Linux distributions with a desktop environment, such as GNOME, KDE, or Xfce.

To access the System Monitor app:

  1. Open the Applications menu or dashboard.
  2. Search for "System Monitor" or go to the System Tools category.
  3. Launch the System Monitor app.

The System Monitor app displays various system metrics, including CPU usage, memory usage, network activity, and disk I/O. In the Memory tab, you can view the following information:

Metric Description
Total memory The total amount of physical memory (RAM) installed on the system.
Used memory The amount of memory currently in use by applications and the OS.
Cached memory The amount of memory used for caching disk data to improve performance.
Buffered memory The amount of memory used for temporary storage of raw disk blocks.
Free memory The amount of memory currently not in use.
Swap usage The amount of swap space used and available.

Example

Let's say you have a Linux system with 8 GB of total memory. When you open the System Monitor app, you see the following memory usage breakdown:

  • Used memory: 4.5 GB
  • Cached memory: 1.2 GB
  • Buffered memory: 0.3 GB
  • Free memory: 2 GB

This shows that out of the 8 GB total memory, 4.5 GB is actively used by applications and the operating system, 1.2 GB is used for caching disk data, 0.3 GB is used for buffering disk blocks, and 2 GB is currently free and available for use.

The System Monitor app also provides a graphical representation of memory usage over time, allowing you to spot trends and potential issues.

GNOME System Monitor

The GNOME System Monitor is a more advanced GUI tool for monitoring system resources and processes. It is part of the GNOME desktop environment but can be installed on other Linux distributions as well.

To launch the GNOME System Monitor:

  1. Open the System menu or application launcher.
  2. Go to the Administration or System Tools category.
  3. Select the "System Monitor" application.

You can also launch the GNOME System Monitor from the command line using the following command:

gnome-system-monitor

The GNOME System Monitor provides detailed information about system resource usage, including memory consumption. In the Resources tab, you can view:

  • Memory and Swap History: A graphical representation of memory and swap usage over time.
  • Memory: The total, used, and free memory, as well as the amount of memory used for caching and buffers.
  • Swap: The total, used, and free swap space.

Example

Suppose you're running a memory-intensive application on your Linux system, and you notice that the system feels slow. You launch the GNOME System Monitor to investigate the issue. In the Resources tab, you observe the following:

  • Total memory: 16 GB
  • Used memory: 14.5 GB
  • Free memory: 1.5 GB
  • Swap used: 2 GB

This information suggests that the memory-intensive application is using a big portion of the available memory, causing the system to use swap space. The high memory usage and swap utilization can lead to reduced system performance.

The GNOME System Monitor also lets you view and manage processes, helping you find applications that may be using too much memory. You can sort the processes by memory usage to pinpoint the most memory-hungry applications and take appropriate actions, such as closing unnecessary programs or optimizing the application's memory usage.

Using GUI tools like the System Monitor app and the GNOME System Monitor, Linux users can easily track and analyze memory usage on their systems. These tools offer a user-friendly interface and visual representations, making it easier to understand and monitor memory consumption.

Other Commands and Tools

In addition to the popular command line and GUI tools we've covered, there are several other tools for monitoring memory usage on Linux systems. One such tool is the btop command.

btop Command

The btop command is a modern, lightweight, and cross-platform resource monitor that runs in the terminal. It provides a colorful interface for viewing real-time usage and statistics for the CPU, memory, disks, network, and processes.

Features of btop include:

  • Customizable interface
  • Ability to filter processes
  • Tree view of processes
  • Navigation using mouse, keyboard, or touch
  • Supports Linux, macOS, and FreeBSD

Installation

To install btop on various Linux distributions, you can use the following package managers:

Distribution Package Manager Installation Command
Debian-based (e.g., Ubuntu) apt sudo apt install btop
Red Hat-based (e.g., CentOS) dnf sudo dnf install btop
Arch Linux pacman sudo pacman -S btop

On other Linux distributions or operating systems, you may need to build btop from source. Refer to the btop installation guide for more information.

Usage

Once installed, you can launch btop by running the following command in your terminal:

btop

In the btop interface, you can view the following memory-related information:

  • Memory usage graph
  • Total memory
  • Used memory
  • Free memory
  • Buffers
  • Cached memory
  • Available memory
  • Swap usage