How to Check Memory in Linux [6 Easy Ways]


To check the memory in Linux, you can use any of the following methods:

  1. free Command to have a quick overview of memory statistics for RAM and swap.
  2. /proc/meminfo to see detailed memory statistics, enabling a deeper understanding of memory allocation and usage.
  3. vmstat Command to get an overview of system memory, CPU, and I/O usage, helping identify potential memory bottlenecks.
  4. top Command to display the real-time system statistics, including memory usage by processes, enabling better process and resource management.
  5. htop Command to view an interactive, user-friendly alternative to the top command with color-coded output, easy navigation, and process management features.
  6. 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:

  1. Use free -h to display memory statistics in a human-readable format.
free -h
  1. The output displays total, used, free, shared, cache, and available memory for both RAM and swap.
output displays total and available memory for ram and swap
  1. Use free -s [interval] to refresh memory statistics at a specified interval (in seconds).
free -s 10
  1. To display the output in a human-readable format, add the -h flag: 
free -h -s 10
  1. You’ll see the following output at every 10 seconds interval in the Terminal window:
you will see output at every ten seconds interval

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:

  1. Use cat /proc/meminfo to display the contents of the file.
cat /proc/meminfo
  1. Look for MemTotal, MemFree, MemAvailable, Buffers, Cached, and SwapTotal.
look for memtotal 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:

  1. Use vmstat -s to display memory statistics.
vmstat -s
  1. The output includes information on total, free, used, and swap space.
output includes information on total memory usage on Linux
  1. 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:

  1. Enter the top command in the Terminal to launch the memory view.
  1. The %MEM column shows the memory usage percentage of each process. Use the < and > keys to navigate and sort columns.
mem column shows memory usage percentage
  1. Press m to sort processes by CPU memory usage.
press m to sort processes by cpu memory usage
  1. Press u followed by a username to display processes for a specific user.
press u followed by username to display processes

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:

  1. In the command prompt, enter the command:
  1. You’ll see the following output:
command prompt enter htop
  1. Press F2 or S to access the setup menu, where you can customize the columns displayed.
press f 2 or s to access setup menu
  1. Use F6 to sort processes by various criteria, including memory usage.
use f 6 to sort processes by various criteria
  1. Press F3 to search for a specific process.
press f 3 to search for 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.

sudo apt install htop to use htop command

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:

  1. Navigate to the applications at the left-bottom of your screen and click it to open the app list installed on your system.
click it to open app list installed on your system
  1. Search for the System Monitor in the list via the search bar at the top.
search for system monitor in list via search bar
  1. Once found, click it to open and view the memory resources details in its Resources tab.
view 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. Use swapon and swapoff 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 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.