Memory management is a vital aspect of keeping a Linux system running smoothly.
Whether you’re a system administrator, a developer, or just a curious user, knowing how to check memory use in Linux is critical for guaranteeing the stability and performance of your system. RAM is a valuable resource, and monitoring its utilization can assist you in identifying and resolving issues such as memory leaks or low memory because of resource-hungry processes.
This post will look at various ways of monitoring and checking memory use in a Linux environment. Whether you prefer command-line tools or graphical interfaces, by the end of this article, you’ll have a firm grasp on assessing your system’s memory utilization while keeping your Linux system running smoothly.
- The Prerequisites to Checking Available Memory in Linux
- Five Methods to Check Memory Usage in Linux
- Method #1: Use the cat Command to Show Linux Memory Information
- Method #2: Use the free Command to See the Physical and Swap Space Memory Capacity
- Method #3: Use the vmstat Command to See Virtual Memory Usage
- Method #4: Use the top Command to Display Memory and CPU Usage
- Method #5: Use the htop Command to See Each Process’s Memory Load
- Use the GUI to Check Linux Memory Use
Let’s start with the prerequisites and then move on to the five ways to check memory usage in Linux.
The Prerequisites to Checking Available Memory in Linux
Here’s what you need to try out the five methods we’ll discuss in this tutorial:
- A system running a Linux distribution
- Access to a command line or a terminal
- A user account with sudo permissions
- The system should have a package manager (we’ll use APT)
System memory (RAM) is just one of the critical specifications you need for smooth server operation. Here’s our guide to choosing the best dedicated server resources for your projects.
Five Methods to Check Memory Usage in Linux
Like most tasks, Linux offers several ways of checking the available system memory. We’ll discuss five simple ways to check memory usage in Linux environment,
Method #1: Use the cat Command to Show Linux Memory Information
The easiest method is to use the cat command to view the contents of the /proc/meminfo file. For this, enter the following commond in the terminal:
$ cat /proc/meminfo
This file tracks how much memory is available and consumed by various processes. It has real-time data on the system’s memory utilization and the kernel’s use of shared memory and buffers. Since this data is closely tied to the system, the outcome could vary slightly depending on the architecture and operating system.
For instance, here’s how the /proc/meminfo file looks like on an Ubuntu machine:
You can see that the system uses kilobytes as the unit for system memory. You have to do some quick math to translate the numbers in GBs.
Method #2: Use the free Command to See the Physical and Swap Space Memory Capacity
Free is a simple command that prints the available and consumed memory statistics. Here’s a sample output:
Like the first method, the output shows the swap and used/available memory in kilobytes.
Let’s explore this output in detail to find out what each caption indicates.
|total||The total physical memory installed on the system|
|used||The memory used by the processes (used = total – free – buff/cache)|
|free||The memory that isn’t currently in use (free = total – used – buff/cache)|
|shared||The memory shared by system processes|
|buffers||The memory the OS has set aside to allot as buffers when processes raise a demand|
|buff/cache||Buffers + Cache|
|available||The available memory new apps can use without swapping.|
As you can see, the free command gives less information than the /proc/meminfo file. However, it is easier to comprehend. The crucial metric is the available value, which shows how much RAM is still available for new applications.
You can customize the output of the command by adding flags at the end. Here are some useful flags for the command.
|-b||Changes memory usage/availability figures to bytes|
|-k||Changes memory usage/availability figures to kilobytes|
|-m||Changes memory usage/availability figures to megabytes|
|-g||Changes memory usage/availability figures to gigabytes|
|-l||Shows detailed low and high memory statistics|
|-o||Revert output to an old format (no -/+buffers/cache line)|
|-t||Shows the total sum of RAM + swap|
|-s||Updates the output every [delay] seconds|
|-c||Updates the output by [count] times|
Method #3: Use the vmstat Command to See Virtual Memory Usage
vmstat (Virtual Memory Statistic Reporter) offers comprehensive insights into system performance aspects, including processes, memory usage, paging, block IO, traps, and CPU activity.
Here’s a sample output of the command:
This output can confuse first-time users because of the details. Let’s go into these details to see what these headings represent.
- Procs (Processes)
- r: the number of processes pending execution.
- B: The number of uninterrupted sleep processes
- swpd: The utilized virtual memory size.
- free: The unused memory space.
- Buff: The portion of RAM used as buffers.
- cache: The RAM space used for caching.
- si: swapped-in memory from disc (/s).
so: swapped memory for disc (/s)
- bi: Blocks received per second (blocks/s) from a block device.
- bo: Blocks per second (blocks written to a block device)
- CPU (These are percentages of total CPU time)
- us: running time devoted to non-kernel code. (User time, pleasant time included)
- sy: Duration of kernel execution. (Time on system)
- id: Length of downtime.
- wa: The amount of time spent waiting out for IO.
- st: Virtual machine time that has been stolen.
Method #4: Use the top Command to Display Memory and CPU Usage
The top command shows detailed information about memory and CPU usage by process. It shows the following details about the system processes:
- average weight
- task execution,
- Count of users who have logged in
- CPU usage and utilization
- swap system processes, and memory
Since the command constantly updates the data, you can use it to view memory consumption as it happens.
The top command shows a near real-time view of the running processes. You can use various flags to see specific information about how processes consume system memory and CPU. We recommend using the man top command to see the list of these flags and options.
Method #5: Use the htop Command to See Each Process’s Memory Load
The htop command has a pretty similar output to the top command we discussed in the previous entry. The main benefits of using the htop commands are its user-friendly interface and improved control options.
Users prefer this command over the top command because of the multiple flags, the option to scroll vertically and horizontally, and the colorful output.
Let’s see the command in action.
Let’s go into the details of this output:
The top part gives a summary of the system’s resource usage. This data gives you a quick overview and simplifies how to check memory usage in Linux systems.
The lower portion contains the details for every running process. This detailed breakdown helps you identify processes that are consuming too much memory and CPU time.
You can further change the details and format of the output by using the commands listed at the bottom.
You should note that htop is not installed by default on some Linux systems. If that’s the case with you, simply install it by running the following command :
$ sudo apt install htop
Do you know that not enough memory is often a common reason why servers lag and crash. That’s why we have included server specifications in our detailed overview of managed dedicated servers.
Use the GUI to Check Linux Memory Use
Command line tools are quick, but they often don’t have user-friendly output. If you prefer the GUI over the command line, you can use the System Monitor utility to visually check memory usage in Linux systems.
We’ll use Ubuntu to demonstrate, but the process of launching and using the utility remains the same on all distros.
Launching System Monitor is easy – simply go to Show Applications. Type System Monitor into the search box to open the utility.
Once the program is active, click the Resources tab. You can see a graphic representation of the memory usage in real time. You can also view historical data for better comparison and analysis.
Keeping track of your Linux system’s memory utilization is essential for maintaining its health and maximizing its performance. We described five simple commands you can try to check memory usage in Linux environments. By routinely utilizing these commands, Linux users and administrators can maintain their system’s effectiveness and responsiveness while avoiding potential performance concerns.
How to check memory usage in Linux is just one of the skills our support team uses every day to ensure our users have a high-performing bare metal infrastructure for their projects.
So, if you’re looking for a robust server for your Ubuntu projects, RedSwitches offers the best dedicated server pricing and delivers instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.
Q1: Why is monitoring memory usage significant in Linux?
A1: Monitoring memory usage is crucial in Linux to ensure efficient resource allocation, prevent performance issues, and troubleshoot system bottlenecks. It helps maintain system stability and responsiveness.
Q2: What is the free command, and how can I check memory usage?
A2: The free command displays information about total, used, and free memory in your system. Running free with options like ‘-m’ (megabytes) or ‘-g’ (gigabytes) provides memory statistics in a more human-readable format.
Q3: How does the top command help in monitoring memory usage?
A3: top’ is an interactive command-line utility that provides real-time information about system processes, including memory usage. It shows the processes consuming the most memory and updates the data continuously.
Q4: What is htop, and how does it differ from top?
A4: htop is an improved version of top with a more user-friendly interface and additional features. It allows you to interactively monitor and manage processes, memory usage, and system performance.
Q5: How can the vmstat command be used to check memory statistics?
A5: vmstat provides a wide range of system statistics, including memory usage. Running vmstat -s displays a summary of memory statistics, while vmstat -m provides information about memory usage in megabytes.
Q6: What does the pmap command do, and how can it be helpful?
A6: pmap is used to display the memory map of a process. It shows the memory regions and their sizes, which can help you analyze a specific process’s memory usage.
Q7: Are there graphical tools available for monitoring memory usage in Linux?
A7: Yes, you can use graphical tools like System Monitor, htop, and GNOME System Monitor to get a visual representation of memory usage and easily identify memory-hungry processes.
Q8: How often should I check memory usage on my Linux system?
A8: Regularly monitoring memory usage is advisable, especially in production environments. Automated monitoring and alerts can help you promptly respond to memory-related issues.
Q9: What actions can I take if I notice high memory usage?
A9: If you observe high memory usage, you can identify and optimize memory-hungry processes, add more RAM to your system, or explore memory management solutions like swapping or caching.
Q10: Are there any tools for long-term memory usage analysis in Linux?
A10: Yes, tools like sar (System Activity Reporter) and Prometheus can be used for long-term memory usage analysis, helping you track performance trends over a period of time.