Get 50% Discount Offer 26 Days

Recommended Services
Supported Scripts
WordPress
Hubspot
Joomla
Drupal
Wix
Shopify
Magento
Typeo3
5 Best Commands to Check Memory Usage in Linux

In the case of Linux, Memory Usage in Linux monitoring for the smooth operation of the system and optimization becomes extremely important for the users. The question of checking memory usage is important, no matter if you’re the guy building an admin site for a server or the guy doing things with memory-intensive applications. Next, you will learn about 5 of the best commands to check memory usage in Linux.

Each command serves a unique purpose so that you can select the most appropriate one based on your specific needs.

1. Free Command

One of the easiest ways to check memory usage in Linux is through the free command. The output provides details on how the system utilizes available memory and the total memory capacity. The simplicity and speed with which it will give you a quick snapshot of memory statistics make this command a popular one. It is one of the glorious commands for memory monitoring in the Linux world and a Linux admin. Also, by understanding its output, you can keep track of resource usage, and use this knowledge to do efficient system management. Of course, the free command is very handy when you want to make sure you’ve got enough free memory without digging into extensive logs.

How to Use the Free Command

To check Memory Usage in Linux with the free command in Linux, open your terminal and type free -h. The -h option makes the output more human-readable by displaying the memory information in MB or GB rather than raw bytes. 

free -h
Command free -h output showing human-readable memory usage in Linux

This command is a quick way to get an overview of the memory allocation in the system, allowing users to understand how much RAM is currently being used, available, or cached, all in an easy-to-read format. It’s a simple step for assessing memory distribution in Linux environments.

Understanding the Output

The output of the free command typically includes:

  • Total Memory: Total amount of RAM available in the system.
  • Used Memory: Amount of RAM currently being used.
  • Free Memory: RAM that is currently not in use.
  • Shared Memory: Memory shared among processes.
  • Buffer/Cache: Memory used by the system for buffers and cache.
  • Available Memory: Amount of memory that can be used without swapping.

Each value helps understand the distribution of memory resources across the system.

Advantages of Using Free Command

Its simplicity and the efficiency of the free command are the advantages. Because it’s Simple and Quick, it’s an excellent tool for performing quick diagnostics of Memory Usage in Linux without a complex setup.

Results will display with MB or GB, which is easier to understand using the Human-Readable Format by using the -h flag.

Additionally, it offers a Real-Time Snapshot of memory, allowing users to observe memory status and make informed decisions instantly. 

With regard to these features, a free, handy tool for users who need immediate insight into what’s going on behind the scenes with the memory above allocation and availability on Linux systems.

2. Top Command

The top command is also a handy tool for real-time monitoring of system processes and Memory Usage in Linux. This command displays an ever-updating list of the activity level of processes, mentioned by CPU and memory utilization, sorted by activity level. The top command is particularly useful for finding resource-intensive applications. Unlike the free command, which only provides a memory overview, the top gives detailed information about processes. Linux Administrators should know the Top commands to monitor the Linux system’s health and identify the performance bottleneck while managing active processes efficiently.

Running the Top Command

To execute the top command, open your terminal and type:

top
Command top output showing real-time system process and memory usage statistics in Linux

Instantly, it shows you a dynamic, real-time view of your system’s active processes. The output consists of an entry for each process in which we see CPU and Memory Usage in Linux, process ID (PID), and the user running the process. The system monitors the performance constantly, using the information that is refreshed every few seconds. In real-time, it helps you identify high resource consumption and locate the problem. The top command is widely used for troubleshooting, as it allows you to see what is happening exactly.

Key Sections in the Top Output

The top output includes several key sections that display essential information about Memory Usage in Linux by each process.

  • %MEM: This shows the percentage of total Memory Usage in Linux by each process, allowing quick identification of high-memory consumers.
  • RES: This indicates the resident memory size, which is the actual physical memory each process occupies.
  • VIRT: It represents the virtual memory size allocated to each process.

Additionally, in this section, you will find other process-related information pertaining to CPU usage and many other bits that make the Top a very useful tool to keep track of system performance and identify processes that need to be analyzed or optimized.

Customizing Top Output for Memory Focus

If you want to focus specifically on memory, press Shift + M to sort processes by Memory Usage in Linux. This shortcut sorts all processes by memory consumption, helping users quickly identify the applications that are using the most memory. This customization is particularly useful for troubleshooting and optimizing memory allocation, as it provides an organized view of memory usage by each active process.

By focusing on memory-intensive processes, users can make informed decisions to improve system performance or identify issues before they affect overall functionality.

Advantages of Top Command

Real-time updates alone make the top command a great choice, as the top command more or less provides the system as it is at that moment. This feature is particularly suited for dynamic monitoring as users can know immediately when system resource usage changes.

And top offers a Customizable View that sorts in various ways, namely memory or CPU usage, which allows you some flexibility in monitoring. , if you want to focus on memory.

In addition, Its Detailed Process Information helps diagnose performance problems by providing details regarding individual processes, and is thus a useful tool to system administrators and Linux users alike.

3. Vmstat Command

A VMSTAT (virtual memory statistics) command is a powerful tool to monitor system performance. It gives some really good memory and CPU, process statistics insights of the entire system, and is a must for any system monitoring. Using vmstat, we can show Memory Usage in Linux, CPU load, and processes from vmstat. It’s particularly useful for diagnosing performance issues and even for learning how resources are allocated throughout the system as a whole — exactly the type of thing that Linux admins can proactively search or fix for potential bottlenecks so that systems run a bit smoother and are a bit more efficient overall.

Using Vmstat to Monitor Memory

To use vmstat for an immediate snapshot of Memory Usage in Linux, simply type:

vmstat -s
Command vmstat -s output showing detailed memory usage and system statistics

It is a command to get a summary of memory-related info, how much total memory, how much used memory, how much free memory, etc. This data can be displayed using the -s option in a simple format so that the state of memory resources can be inspected quickly by users. This overview is helpful for both real-time monitoring and troubleshooting memory-related issues, as it gives a clear, immediate picture of memory availability and usage within the system without needing to filter through extensive logs.

For continuous memory monitoring, the vmstat command can be used with intervals and counts, for example.

vmstat [interval] [count]

You can set how frequently and how many times the memory statistics should be updated. This method is useful for analyzing Memory Usage in Linux trends over a specific period.

For example, 

vmstat 5 10

It will display memory statistics every 5 seconds for a total of 10 times. 

Command vmstat 5 10 showing memory statistics every 5 seconds for 10 times in Linux

This feature will aid users in watching for unusual memory patterns and performance issues whose occurrence can be recorded in real-time, which can be useful when trying to troubleshoot things or optimize resource management.

Key Metrics in Vmstat Output

The vmstat command output includes several key metrics that are essential for analyzing system performance.

  • Memory Statistics cover free memory, buffer, and cache usage, providing a clear picture of memory availability.
  • Swap Activity: displays data on swap-in and swap-out activity, indicating how often the system uses swap memory, which can affect performance.
  • CPU Usage: shows CPU idle time, user time, and system time, helping users identify if the CPU is overutilized or underutilized.
  • Disk I/O: reflects read and write operations, which are critical for understanding how disk access may be impacting system responsiveness.

Advantages of Vmstat Command

The vmstat command offers several advantages, making it a valuable tool for system monitoring.

  • Comprehensive Output: combines memory, CPU, and disk I/O statistics, providing a well-rounded view of system performance.
  • Customizable Interval: option allows users to set specific update frequencies, enabling real-time monitoring that suits their needs.

Additionally, vmstat is known for its Minimal Resource Usage, making it lightweight and ideal for continuous monitoring without significantly impacting system performance.

These features make vmstat a preferred choice for Linux administrators seeking efficient, real-time insights into system resources.

4. Ps Command

Linux ps command is a process status tool with great power that allows us to deeply understand those processes, how much of the Memory Usage in Linux, and the CPU. With PS, users are able to monitor resource consumption, track individual processes, and identify any such applications using excess memory. This command is especially useful for process management on the detailed level, as well as process troubleshooting, because based on process status, it makes it possible for an administrator to perform the necessary actions. Although the PS command is flexible and can be customized to use with different options, it is useful for the process diagnostics of the Linux environment.

Basic Usage of Ps for Memory Monitoring

To check Memory Usage in Linux by processes, use:

ps aux --sort=-%mem

This command lists all running processes and sorts them by Memory Usage in Linux, with the processes consuming the most memory appearing at the top. The PS aux options display extensive details about each process, such as user, memory usage, CPU usage, and command name, giving a comprehensive view of system activity. By using this sorted view, users can quickly identify memory-intensive processes and manage resources effectively, which is helpful for optimizing performance and addressing memory-related issues.

Command ps aux --sort=%mem showing processes sorted by memory usage in Linux

Understanding Ps Output Columns

The output of the ps command includes several important columns that offer detailed insights into Memory Usage in Linux by processes. 

  • %MEM indicates the percentage of physical memory used by each process, which helps identify memory-intensive applications. 
  • VSZ represents the virtual memory size, showing the total memory allocated, including both RAM and swap. 
  • RSS stands for Resident Set Size, which is the portion of memory that is currently in physical RAM and not swapped out. 

These columns allow users to get a clear view of how each process utilizes memory resources, aiding in efficient system monitoring.

Filtering Ps Output for Specific Processes

The ps command can be customized to filter output and display only processes related to a specific application. For example, to check memory usage for processes associated with a web server like Nginx, you can use

ps aux | grep nginx
Command ps aux | grep nginx filtering processes to show Nginx-related memory usage

This command will display information about only those processes that include “nginx” in their command name or path. Filtering PS output helps users focus on specific applications or services, making it easier to troubleshoot or monitor memory usage for individual processes without sorting through irrelevant entries.

Advantages of Ps Command

  • Detailed Process Information: This allows users to get deep data on each process, such as memory and CPU usage, as well as user and command details, which are needed for comprehensive monitoring.
  • Sorting and Filtering Options: PS enables customization to display only relevant processes, making it adaptable to specific monitoring needs. 

This feature helps administrators and Linux users dealing and monitoring processes individually to come under control of resource allocation and performance troubleshooting.

5. Htop Command

The htop command is an interactive process monitoring tool, similar to the top command but with a more user-friendly interface and enhanced features. Unlike top, htop provides a colorful, organized view of system processes, making it easier to navigate and interpret. With additional functionalities such as scrolling and process management through keyboard shortcuts, htop offers a more intuitive experience for monitoring CPU, memory, and process activity. This tool is popular among users who prefer a visual and interactive approach to managing system resources and assessing performance.

Launching Htop

To use htop, enter:

htop
Command htop output showing real-time system process and resource usage

This command executes the htop interface to show you a real-time view of the CPU, memory, and swap usage, top, and listed active processes. The information in each process entry includes process ID, user, memory usage, and command for complete insight into system activity. Use arrow keys to navigate processes actively and more efficiently through this interface to monitor the processes you wish and manage resources efficiently. If you want to see that list in a more visually oriented process monitor, look no further than Htop.

If it’s not installed, you can add it to most Linux distributions by running:

sudo apt-get install htop
Command sudo apt-get install htop to install htop for real-time process monitoring

Navigating the Htop Interface

The htop interface is designed with a colorful and intuitive layout, making it easier to monitor system resources.

It includes various elements, such as 

Memory Usage Bar that graphically represents overall memory usage.

Process-Specific Memory information, where users can sort processes by memory usage by pressing F6 and selecting %MEM. 

 Interactive Control with arrow keys for navigation and the F9 key for terminating processes directly.

This layout provides a user-friendly experience, allowing users to assess and manage system performance without complex commands quickly.

Advantages of Htop Command

  • User-Friendly Interface: Enhanced visual experience with color-coded bars.
  • Interactive Controls: Allows for easy process management directly within the tool.
  • Detailed Memory Information: Provides comprehensive memory and CPU usage data.

Additional Tips for Optimising Memory Usage in Linux

Monitoring memory is only part of the equation. Optimizing it ensures smoother system performance. Here are a few tips:

  • Clear Cache: Use echo 3 > /proc/sys/vm/drop_caches to clear the cache when needed.
  • Disable Unused Services: Disable background services not in use to free up memory.
  • Optimize Swap: Adjust swap usage by modifying the swappiness parameter in /etc/sysctl.conf.
  • Check Logs for Memory-Intensive Processes: Review logs to identify processes consuming excessive memory over time.

Conclusion

In this guide, we’ve covered five essential commands for monitoring memory usage in Linux: top, htop, free, vmstat, and ps. Each command offers custom features and a memory usage view for your convenience. Monitoring memory usage on a regular basis not only keeps the system almost always perfect, but it also acts as an early warning to troubleshoot and prevent possible problems from becoming bigger and more troublesome.

By incorporating these commands into your workflow, you can maintain a balanced and optimized Linux environment.

About the writer

Vinayak Baranwal Article Author

Vinayak Baranwal wrote this article. Use the provided link to connect with Vinayak on LinkedIn for more insightful content or collaboration opportunities.

Leave a Reply

Your email address will not be published. Required fields are marked *

Lifetime Solutions:

VPS SSD

Lifetime Hosting

Lifetime Dedicated Servers