How To Check Linux Memory Usage

Check out our guide on how to check memory usage, process memory usage, and even check historical memory usage via the Linux command line.

On this page
  1. Introduction
  2. Terms To Know Beforehand
  3. Checking Overall Memory Usage
  4. Checking Per Process Memory Usage
  5. Historical Memory Usage
  6. Conclusion
  7. Additional Links

Memory or random access memory (RAM) is a hardware component of a computer that is used to temporally store data. RAM can be compared to sticky notes and short-term memory. Like sticky notes, RAM acts as a temporary storage area where data and instructions are quickly accessible for immediate tasks, much like sticky notes. RAM is more like your short-term memory when it comes to storage as when a computer is shut down or restarted. After RAM is cleared it takes more time to recall data much like how humans need to think when recovering memories from long-term (think Pixar’s Inside Out).

#Introduction

In this blog post, we will be going over how to check memory usage in Linux from the command line. Covering how to check total memory usage, process memory usage, and ways to even check historical memory usage. However, before we get started I recommend looking at the terms below.

#Terms To Know Beforehand

Physical Memory is the usage of the physical memory (RAM).

Virtual Memory provides programs with the illusion that it has access to large amounts of memory even though the system may be limited.

Swap is storage space on your hard drive/SSD that is reserved as a backup in case memory is full.

Swappiness is the numerical value of how much memory needs to be in use to start using swap memory. Though think of it flipped 0 being do not swap until out of memory and 100 being treat swap the same as RAM.

#Checking Overall Memory Usage

If you are looking to check overall system memory usage I recommend using the free command.

Code
free -h

The number to be focused on is the last line of the memory line the available column.

In this example, 21G of memory is still available meaning things are just fine.

Advanced overall usage from using proc

Another way to check the overall memory usage is by checking out /proc/meminfo. I am calling this more advanced due to the amount of information you have to sort through many times the information the free command displays.

Code
cat /proc/meminfo

Bellow is just the first 16 lines out of the total of 57 lines

In this case, MemAvailable is the same number we looked at before though it’s displayed in kB rather than MB or GB.

#Checking Per Process Memory Usage

Using PS we can check the process ID for usage stats

Code
ps u -p PID

Output

For memory usage refer to the %MEM section which in this example is 1.3% of the system memory

Advanced from proc per pid

Like the previous advanced section, this is pulling information directly from proc. That said let’s use grep to filter things down as the status file contains more than just memory usage information.

Code
cat /proc/PID/status | grep 'VmSize\|VmRSS'

output example

VmSize – is the virtual memory being used by the process.

VmRSS – is the physical memory being used by the process.

#Historical Memory Usage

If you want to look at historical Memory usage will need to have a method of logging the data ahead of time. For now, let’s focus on a simple solution to gather that data.

The sar command is one of the simplest ways to gather historical system data which includes memory usage. It gathers this data once every 10 minutes and then when you run the command displays the information in a table.

To Install

To install sar the package name should be sysstat on most Linux Distributions.

For Debian and Ubuntu systems

Code
apt install sysstat

For RHEH, AlmaLinux, CentOS, and other systems using DNF

Code
dnf install sysstat

To Start And Enable

To make sure it is installed and started run

Code
sudo systemctl enable sysstat && sudo systemctl start sysstat

To Check

To Check historical memory usage use the sar command using the following command

Code
sar -r

To preserve the columns this example is being added as a screenshot.

Screenshot of the partial output from running sar -r

Out of all the information from sar, you will want to focus on the %memused and the %commit columns.

You’re probably thinking why %commit higher? Well, don’t forget that we still have swap memory.

Be aware that swapping to disk is slower. If you run into an issue where memory is over-committed meaning above 100% you may start noticing performance degeneration. If this is occuring often this is either an indication of an issue or a sign you need more memory.

Note: About Gathering CPU Usage Data From Many Servers

If you need data from many servers the general recommended to have a centralized system that either pulls from the servers or is pushed to the centralled collection server.

Using a system like this makes collecting the data quite a bit more complicated, so think carefully before implementing it.

If you decide to go down this route my recommendation is to use promethus. This ends up being made up of a few components the Prometheus exporter, Prometheus, and the dashboard used to display the data.

#Conclusion

In this blog post, we have discussed what RAM is as well as some related terms. From there we discussed how to check memory usage overall, per process, and historically. If you have questions about terms mentioned from proc or the other commands check out the links in the section below for additional documentation.

Done reading and looking for more things to read, why not check these out?

Illustrated avatar of CJ Saathoff
Written by

CJ Saathoff

CJ Saathoff writes for the NameHero blog.

More from CJ →