How to Check Memory Usage in Ubuntu | Optimal Resource Management Guide

1. The Importance of Checking Memory Usage in Ubuntu

1.1 The Role of Memory Management in Ubuntu

Monitoring memory usage is a crucial task in Linux systems like Ubuntu. Checking memory usage is essential for optimizing system performance and ensuring proper resource allocation. If memory runs low, especially on servers or during high-load operations, system performance can degrade significantly, potentially leading to crashes.

Ubuntu provides various tools that make it easy to monitor memory usage. In the following sections, we will explain how to use these tools effectively.

2. Methods for Checking Memory Usage in Ubuntu

2.1 Checking Memory with the free Command

The free command is one of the most common ways to check memory usage in Ubuntu. It allows you to see the total memory, available memory, and memory used for buffers and cache.

free -h

Using the -h option displays the information in a human-readable format (GB or MB). The output includes the following details:

  • Mem: Total memory, used memory, and available memory
  • Swap: Virtual memory usage
  • Cache: Amount of memory used for buffers and cache

This command is lightweight and provides quick access to memory information without putting a load on the system, making it a frequently used tool.

2.2 Checking Memory Visually with the htop Command

The htop command provides real-time visual monitoring of system resources. It is a powerful tool that allows users to intuitively check memory usage for each process through a graphical interface.

sudo apt install htop
htop

When executed, it displays active processes along with their memory usage, CPU usage, swap usage, and more in a visually structured way. Additionally, users can easily sort and terminate processes. The GUI-like interface makes it a popular choice among many users.

2.3 Checking Memory with the top Command

The top command is used to monitor system resource usage in real time. Compared to htop, it operates in a text-based interface but remains a valuable tool due to its low resource consumption.

top

The top command displays processes sorted by resource usage. Pressing Shift + M sorts processes based on memory usage, making it useful for identifying memory-heavy processes that might be slowing down the system.

2.4 Using the /proc/meminfo File

To obtain detailed memory information in Ubuntu and Linux systems, you can access data directly from the /proc/meminfo file. This file provides memory details managed by the system kernel, which can be checked using the cat command.

cat /proc/meminfo

This command displays memory usage, cache, swap memory status, and other detailed information in text format. Since it provides more comprehensive data than the free or top commands, it is useful for advanced memory monitoring.

侍エンジニア塾

3. How to Deal with High Memory Usage

3.1 Identifying and Terminating Unnecessary Processes

If your system is running low on memory, the first step is to identify and terminate memory-consuming processes. Use top or htop to check high-memory processes and stop them if necessary.

kill [Process ID]

The process ID (PID) can be found in the output of the top or htop command.

3.2 Adjusting Swap Memory

When Ubuntu runs out of physical memory, it uses virtual memory (swap space). If your system lacks sufficient swap space, you can create additional swap to improve performance.

Steps to Create Swap Space:

sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

This process expands your system’s memory resources by adding extra swap space.

4. Conclusion

Monitoring memory usage in Ubuntu is essential for maintaining system stability. By utilizing commands like free and htop, you can efficiently track system performance and address memory shortages or excessive resource consumption. Additionally, managing swap space and terminating unnecessary processes allows for effective resource allocation.

侍エンジニア塾