TL;DR
To check the memory in Linux, you can use any of the following methods:
- free Command: Use free -h to display memory statistics in a human-readable format.
- /proc/meminfo: Use cat /proc/meminfo to display detailed memory statistics.
- vmstat Command: Use vmstat -s to display an overview of system memory, CPU, and I/O usage.
- top Command: Use top to display real-time system statistics, including memory usage by processes.
Learn how to check memory usage on Linux systems with these top eight methods and some quick tips for memory optimization.
Memory is one of the most critical resources to manage, especially in Linux-based environments. If left unchecked, you may experience poor performance of your system due to inefficient memory usage.
To avoid that from happening, use this comprehensive guide to understand how to check memory usage on Linux. I will also share some memory optimization tips and ways to automate memory monitoring.
How Linux Uses Memory Differently than Other Operating Systems
Linux uses memory more efficiently compared to some other operating systems. It tries to use as much RAM as possible to cache data, which speeds up system operations. This doesn’t mean Linux is running out of memory, it’s just using available resources efficiently.
Linux dynamically allocates and deallocates memory as needed. It balances memory use between applications, cache, and system processes to optimize performance. Additionally, Linux uses swap space more strategically. It tries to keep active processes in RAM and moves less active processes to swap, maintaining system performance.
Explanation of RAM, Swap, and Cache
- RAM (Random Access Memory): RAM is the primary memory your computer uses to store data that is actively being used or processed. It is fast and allows quick access to data, making your system run smoothly. For example, when you open a program, it gets loaded into RAM for fast access.
- Swap: Swap space acts as an overflow area for your RAM. When RAM is full, less-used data moves to the swap space to free up RAM for more important tasks. Swap is slower than RAM because it uses part of your hard drive or SSD.
- Cache: Cache stores frequently accessed data to speed up future access. It can be part of RAM or the hard drive. For example, the cache might store parts of files or applications you use often, so they load faster next time.
How to Check Memory in Linux [6 Easy Ways]
To check memory in Linux, you can use several commands like free
, top
, htop
, vmstat
, cat /proc/meminfo
, and sar
. The free
command provides a quick overview, while top
and htop
offer real-time monitoring. vmstat
gives detailed statistics, and cat /proc/meminfo
shows detailed memory information. For historical data, use sar
from the sysstat
package.
Keep reading for a detailed step-by-step guide for eight different methods to check memory usage in Linux:
1. free Command
The free command is a simple and widely used tool to check the memory in Linux. Here’s how you can use this command to check the memory in Linux:
- Use free
-h
to display memory statistics in a human-readable format.
free -h
- The output displays total, used, free, shared, cache, and available memory for both RAM and swap.
- Use
free -s [interval]
to refresh memory statistics at a specified interval (in seconds).
free -s 10
- To display the output in a human-readable format, add the
-h
flag:
free -h -s 10
- You’ll see the following output at every 10 seconds interval in the Terminal window:
2. /proc/meminfo
The /proc/meminfo file contains detailed memory statistics. It provides a more concise output while /proc/meminfo
offering more detailed information. To view it, follow the steps below:
- Use
cat /proc/meminfo
to display the contents of the file.
cat /proc/meminfo
- Look for MemTotal, MemFree, MemAvailable, Buffers, Cached, and SwapTotal.
3. vmstat Command
The vmstat
command provides an overview of system memory, CPU, and I/O usage. Here’s how to execute this command in the Linux command prompt:
- Use
vmstat -s
to display memory statistics.
vmstat -s
- The output includes information on total, free, used, and swap space.
- In this output, if you notice high swap usage and low free memory, it means that there is a memory bottleneck that needs to be fixed.
4. top Command
The top command displays real-time system statistics, including memory usage by processes. To access this memory view, follow these steps:
- Enter the
top
command in the Terminal to launch the memory view.
top
- The %MEM column shows the memory usage percentage of each process. Use the < and > keys to navigate and sort columns.
- Press m to sort processes by CPU memory usage.
- Press u followed by a username to display processes for a specific user.
5. htop Command
The htop
command offers an interactive and user-friendly alternative to the top command. You can see the color-coded output, use easy navigation, and process management features. Follow the steps below to run this command in the Terminal:
- In the command prompt, enter the command:
htop
- You’ll see the following output:
- Press F2 or S to access the setup menu, where you can customize the columns displayed.
- Use F6 to sort processes by various criteria, including memory usage.
- Press F3 to search for a specific process.
Based on the Ubuntu version installed on your system, you may need to install htop with the sudo snap install htop
or sudo apt install htop
to use the htop
command.
6. cat Command
The cat command can be used to read and display the contents of the /proc/meminfo file, which contains detailed information about your system’s memory usage.
- Open your terminal application.
- Type the following command:
cat /proc/meminfo
This command will display detailed memory information, including total and free memory.
- To monitor memory in real-time, use:
watch cat /proc/meminfo
This command will update the memory information every two seconds.
The output will be:
7. Using the sar Command
The sar command, part of the sysstat package, is a powerful tool for monitoring system performance, including memory usage. It can provide both real-time and historical data.
- If sar is not installed, install it by running:
sudo apt-get install sysstat
This command installs the sysstat package, which includes the sar tool.
- Enable and start the sysstat service to begin collecting data:
sudo systemctl enable sysstat
sudo systemctl start sysstat
These commands ensure that the sysstat service starts on boot and begins collecting data.
- To view memory usage, type:
sar -r 1 5
This command will report memory usage every second for 5 intervals.
The output will include columns such as kbmemfree, kbmemused, memused%, kbbuffers, kbcached, kbcommit, and commit%.
- Look at the %memused column to see the percentage of used memory.
- The kbmemfree column shows the amount of free memory in kilobytes.
- The kbcached column indicates the memory used by the page cache.
6. GUI-based tools
Several graphical tools are available for memory monitoring on Linux. However, you can use the Gnome System Monitor. It is available on all GNOME-based distributions like Ubuntu and provides a more user-friendly interface for monitoring memory usage. Here’s how you can access the Gnome System Monitor on your Ubuntu:
- Navigate to the applications at the left-bottom of your screen and click it to open the app list installed on your system.
- Search for the System Monitor in the list via the search bar at the top.
- Once found, click it to open and view the memory resources details in its Resources tab.
For KDE-based distributions like Kubuntu, the KDE System Monitor offers similar functionality as the Gnome System Monitor.
3 Best Ways to Automate Memory Monitoring
Automate memory monitoring on Linux with cron jobs, custom scripts, and monitoring systems. Here’s how you can do it:
- ⚙️ Setting Up Cron Jobs for Regular Checks: Use crontab -e to open the cron table for editing. Add a new line with the desired time interval and command to run, such as
*/5 * * * * /usr/bin/free -h >> /home/user/memory_stats.log
to log memory statistics every 5 minutes. - 📜 Creating Scripts for Custom Memory Monitoring: Write a script in your preferred scripting language (e.g., Bash, Python) to extract specific memory statistics. Schedule the script to run at desired intervals using cron jobs or other scheduling tools.
- 🖥️ Integrating with Monitoring Systems and Alerting Tools: Configure monitoring tools like Nagios or Zabbix to collect memory usage data. Set up alerting thresholds to receive notifications when memory usage exceeds specified limits.
4 Quick Tips to Optimise Memory Usage on Linux
You can use some quick tips to optimize your system’s memory usage. That includes identifying memory-hogging processes, configuring swap space, and fine-tuning settings. Here are the details for it:
- 🔍Identifying Memory-hogging Processes: Use tools like
top
,htop
, or other methods to identify processes consuming excessive memory. Investigate the cause of high memory usage and take appropriate action, such as terminating the process, adjusting configuration, or updating the software. - 🔧Configuring Swap Space and Swappiness: Adjust the
vm.swappiness
kernel parameter to control how aggressively the system swaps data between RAM and swap space. Useswapon
andswapoff
commands to enable or disable swap space as needed. - 💻Tuning Application and System Settings: Adjust application settings to limit memory usage, such as reducing cache sizes or disabling unnecessary features. Optimize system settings, including file system caching and kernel parameters, to improve overall memory usage.
- 💾Upgrading Hardware Components: Upgrading hardware can improve computer performance. Adding more RAM can increase data handling, resulting in faster multitasking. Adding more swap space can also accommodate memory-intensive applications. It’s important to balance the amount of added hardware to avoid performance issues.
Final Thoughts
You’ve learned the top eight methods for checking memory in Linux, along with practical examples, best practices, and some optimization tips. Regular memory monitoring and optimization are essential for maintaining peak system performance.
For more learning, you can explore:
- Techniques to get CPU information on Linux, which will help you keep track of your system’s performance.
- How to install and use
htop
to improve your system monitoring capabilities. - Methods to kill processes from the Linux command line to manage rogue applications effectively.