To check the memory in Linux, you can use any of the following methods:
freeCommand to have a quick overview of memory statistics for RAM and swap.
/proc/meminfoto see detailed memory statistics, enabling a deeper understanding of memory allocation and usage.
vmstatCommand to get an overview of system memory, CPU, and I/O usage, helping identify potential memory bottlenecks.
topCommand to display the real-time system statistics, including memory usage by processes, enabling better process and resource management.
htopCommand to view an interactive, user-friendly alternative to the top command with color-coded output, easy navigation, and process management features.
- GUI-based tools, like Gnome System Monitor and KDE System Monitor, offer user-friendly interfaces with more visual representations for memory monitoring on Linux systems.
You can also automate memory monitoring tasks with cron jobs, use custom scripts, and try monitoring systems. Furthermore, you should identify memory-hogging processes, configure swap space, and fine-tune the application and system settings to optimize memory usage on your Linux system.
Learn how to check memory usage on Linux systems with these top six 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 to Check Memory in Linux [6 Easy Ways]
To check the memory in Linux, I will explore the top 6 methods. That includes commands: free, /proc/meminfo, vmstat, top, and htop. Furthermore, you’ll get to look into GUI-based tools that offer a more user-friendly and graphical view of the memory consumption on your Linux system.
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
-hto display memory statistics in a human-readable format.
- The output displays total, used, free, shared, cache, and available memory for both RAM and swap.
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
free -h -s 10
- You’ll see the following output at every 10 seconds interval in the Terminal window:
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:
cat /proc/meminfoto display the contents of the file.
- Look for MemTotal, MemFree, MemAvailable, Buffers, Cached, and SwapTotal.
3. vmstat Command
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:
vmstat -sto display memory statistics.
- 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
topcommand in the Terminal to launch the memory view.
- 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
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:
- 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
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.logto 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
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.swappinesskernel parameter to control how aggressively the system swaps data between RAM and swap space. Use
swapoffcommands 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.
You’ve learned the top six 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.
I encourage you to explore additional system monitoring tools and techniques to enhance your Linux environment further. You could also experiment with various other optimization techniques, such as customizing quick access to apps via desktop shortcuts, managing apt-get upgrade and dist-upgrade, and running binary files to experience the efficient performance of your Linux system. By continuing to learn and explore, you can become a more proficient Linux user and keep your system running smoothly.
Frequently Asked Questions
Can you explain the key components of memory in Linux?
Absolutely! The key components of Linux memory are RAM and Swap Space, Cached, Buffered, and Free Memory, and Virtual and Physical Memory. RAM stores data and programs in use, while Swap Space extends RAM to temporarily store inactive data on the disk. Cached Memory speeds up disk data access, Buffered Memory temporarily stores data being transferred between devices, and Free Memory is available for new processes or data. Virtual Memory enables processes to use more memory than is physically available, while Physical Memory refers to the actual memory hardware in a system.
How do I know if my system has a memory leak?
A memory leak occurs when a program fails to release the memory it no longer needs. Monitor memory usage over time using tools like top or htop. If memory usage consistently increases without a clear reason, you may have a memory leak.
What are the performance implications of using swap space in Linux?
Using swap space can help prevent your system from running out of memory, but it comes at the cost of reduced performance. Swap space is typically located on slower disk drives, so moving data between RAM and swap space can result in longer access times compared to using RAM alone. If your system frequently relies on swap space, consider upgrading your RAM or optimizing memory usage to improve performance.
How do I allocate more memory to a specific process in Linux?
In Linux, you generally cannot directly allocate more memory to a specific process. However, you can adjust the process’s priority using the nice or renice commands, which can indirectly impact memory allocation. Lower-priority processes may receive fewer resources, including memory, when higher-priority processes demand them. Additionally, you can optimize the process’s memory usage by modifying its configuration or settings to ensure the efficient use of available resources.