How to Check Memory in Linux [8 Easy Ways]

Written by

Reviewed by

Last updated: July 24, 2024

Expert verified

SVG Image

TL;DR

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

  1. free Command: Use free -h to display memory statistics in a human-readable format.
  2. /proc/meminfo: Use cat /proc/meminfo to display detailed memory statistics.
  3. vmstat Command: Use vmstat -s to display an overview of system memory, CPU, and I/O usage.
  4. 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:

  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.
top
  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:
htop
  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. 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.

  1. Open your terminal application.
  2. Type the following command:
cat /proc/meminfo

This command will display detailed memory information, including total and free memory.

viewing detialed memory information
  1. To monitor memory in real-time, use:
watch cat /proc/meminfo

This command will update the memory information every two seconds.

using cat to view realtime memory

The output will be:

viewing realtime memory info

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.

  1. 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.

installing sysstat package on ubuntu
  1. 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.

enabling and starting sysstat services
  1. 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.
viewing memory usage using sar

    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 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:

    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.

    Ojash

    Author

    Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

    Akshat

    Reviewer

    Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

    Share this article
    Shareable URL
    Prev Post

    3 Quick Ways to Fix the “wget command not found” Error

    Next Post

    4 Easy Ways to Create Desktop Shortcuts on Ubuntu

    Read next