Skip to content

Memory

A Detailed Guide to Monitoring RAM in Linux

Monitoring RAM usage is crucial for understanding system performance, diagnosing issues, and ensuring that applications have the resources they need. In Linux, several utilities allow you to monitor RAM usage, analyze memory allocation, and identify potential bottlenecks. This guide provides an overview of key tools for monitoring RAM, with detailed examples and explanations.


free

Description:
The free command provides a quick overview of the system's memory usage, including information on total, used, free, shared memory, and the buffer/cache.

Basic Usage:

free -h
  • -h: Outputs the memory usage in a human-readable format (e.g., KB, MB, GB).

Detailed Usage:

free -m
  • -m: Displays memory usage in megabytes.

Understanding the Output:

  • Total: The total amount of physical RAM available.
  • Used: The amount of RAM currently in use.
  • Free: The amount of RAM that is currently free.
  • Shared: Memory used (mostly) by tmpfs (temporary file system).
  • Buffers: Memory used by kernel buffers.
  • Cache: Memory used by the page cache and slabs.
  • Available: An estimate of how much memory is available for starting new applications without swapping.

The buffer/cache section is crucial because it shows memory that is being used for buffering and caching by the operating system to improve performance. This memory is technically in use but can be quickly freed up for other purposes if needed, which is why the "Available" value is typically higher than the "Free" value.


top

Description:
top is an interactive command-line utility that displays a dynamic view of system processes, including memory and CPU usage.

Basic Usage:

top

Understanding the Memory Section:

At the top of the top display, you’ll see memory usage statistics:

  • KiB Mem: Total memory statistics.
  • total: Total amount of RAM.
  • used: RAM currently used by processes.
  • free: RAM not in use by any processes.
  • buff/cache: Combined memory used by the kernel buffers and the page cache.
  • available: Approximate memory available for new processes.

Sorting by Memory Usage:

To sort processes by memory usage, press the M key while top is running.


htop

Description:
htop is an enhanced version of top, providing a more user-friendly, color-coded, and interactive interface for monitoring processes and memory usage.

Basic Usage:

htop

Features:

  • Memory Graph: At the top of the interface, htop shows a bar graph for memory usage, with different colors representing different types of memory usage (e.g., buffers, cache, used).
  • Interactive Sorting: You can easily sort processes by memory or CPU usage by clicking on the column headers or using function keys.
  • Tree View: Shows processes in a hierarchical tree structure, making it easier to see parent-child relationships.

Installation:

If htop is not installed, you can install it using:

sudo apt install htop   # For Debian/Ubuntu
sudo yum install htop   # For CentOS/RHEL

vmstat

Description:
vmstat reports virtual memory statistics, including processes, memory, paging, block I/O, traps, and CPU activity.

Basic Usage:

vmstat 1 5
  • 1: The delay between updates in seconds.
  • 5: The number of updates to display.

Understanding the Memory Columns:

  • 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.
  • si/so: Swap in/out — the amount of memory swapped in or out to disk.

The buff and cache fields are critical for understanding how Linux optimizes memory usage. Buffers are used for I/O operations, while the cache is used to store frequently accessed disk data to speed up access.


smem

Description:
smem provides a more detailed view of memory usage by processes, accounting for both shared and private memory.

Basic Usage:

smem -r
  • -r: Sorts the output by resident memory (RSS).

Detailed Usage:

smem -k -t -r -c "pid user command swap rss"
  • -k: Display memory usage in kilobytes.
  • -t: Display a totals line.
  • -c: Specify columns to display.

Understanding the Output:

  • PID: Process ID.
  • User: The user running the process.
  • Command: The command used to start the process.
  • Swap: Amount of swap used by the process.
  • RSS: Resident Set Size — the portion of memory occupied by a process in RAM.

Installation:

smem might need to be installed:

sudo apt install smem   # For Debian/Ubuntu
sudo yum install smem   # For CentOS/RHEL

ps

Description:
ps is a versatile command used to list processes. It can be combined with other commands to sort and display memory usage.

Basic Usage:

ps aux --sort=-%mem | head -n 10
  • --sort=-%mem: Sorts processes by memory usage in descending order.
  • head -n 10: Displays the top 10 processes by memory usage.

Understanding the Output:

  • %MEM: Percentage of RAM used by the process.
  • RSS: Resident Set Size — the actual physical memory used by the process.

This command is useful for quickly identifying processes that consume the most memory.


glances

Description:
glances is a comprehensive monitoring tool that provides a detailed overview of system performance, including memory usage.

Basic Usage:

glances

Features:

  • Memory Section: Shows detailed statistics on memory and swap usage.
  • Real-Time Monitoring: Continuously updates system performance data.
  • Alerts: Highlights issues like high memory usage with color codes.

Installation:

If glances is not installed, you can install it using:

sudo apt install glances   # For Debian/Ubuntu
sudo yum install glances   # For CentOS/RHEL

watch

Description:
watch is not specifically a memory monitoring tool but can be used to periodically run any command to monitor memory usage over time.

Basic Usage:

watch -n 5 free -m
  • -n 5: Runs the free -m command every 5 seconds.

This is useful for continuously monitoring memory usage with any of the above commands.


Understanding Buffer and Cache in Linux Memory Management

In Linux, memory management includes mechanisms like buffering and caching to optimize system performance:

  • Buffers: Buffers are used by the kernel to manage I/O operations. They store data temporarily while it is being transferred between devices or processes. This helps in managing bursty I/O operations efficiently.

  • Cache: Cache memory stores copies of frequently accessed data from the disk, allowing the system to retrieve this data faster than if it had to read it from the disk every time. This improves overall system performance by reducing the need to access slower disk storage.

The memory used by buffers and cache is technically considered "in use," but it can be quickly freed if needed by the system, making this memory "available" for applications when required.


Conclusion

Monitoring RAM in Linux is critical for ensuring optimal system performance. Tools like free, top, htop, vmstat, smem, ps, and glances provide various perspectives on memory usage, from high-level overviews to detailed per-process memory consumption. Understanding the role of buffers and cache in Linux’s memory management system is also essential for interpreting the output of these tools correctly. By using these utilities effectively, you can gain deep insights into how your system is using its memory resources.