Connect with us

Website

How To Check Linux Memory Usage

Published

on

How To Check Linux Memory Usage

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.

Advertisement

Checking Overall Memory Usage

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

free -h

total used free shared buff/cache available

Mem: 31Gi 9.1Gi 13Gi 1.6Gi 10Gi 21Gi

Swap: 8.0Gi 0B 8.0Gi

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.

Advertisement

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.

cat /proc/meminfo

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

MemTotal: 32540008 kB

MemFree: 13835532 kB

MemAvailable: 23078148 kB

Buffers: 4488 kB

Advertisement

Cached: 10741516 kB

SwapCached: 0 kB

Active: 11198436 kB

Inactive: 4740928 kB

Active(anon): 6811408 kB

Advertisement

Inactive(anon): 0 kB

Active(file): 4387028 kB

Inactive(file): 4740928 kB

Unevictable: 1470544 kB

Mlocked: 5984 kB

Advertisement

SwapTotal: 8388604 kB

SwapFree: 8388604 kB

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

ps u -p PID

Output

Advertisement

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

user 196320 0.0 1.3 563904 54672 ? Ssl Apr17 0:00 /usr/libexec/gnome-terminal-server

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.

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

output example

VmSize: 563904 kB

VmRSS: 54672 kB

Advertisement

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.

Advertisement

For Debian and Ubuntu systems

apt install sysstat

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

dnf install sysstat

To Start And Enable

To make sure it is installed and started run

sudo systemctl enable sysstat && sudo systemctl start sysstat

To Check

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

sar -r

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

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

Advertisement

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?

Stephen Oduntan is the founder and CEO of SirsteveHQ, one of the fastest growing independent web hosts in Nigeria. Stephen has been working online since 2010 and has over a decade experience in Internet Entrepreneurship.

Continue Reading
Advertisement
Comments

Trending

Copyright © 2024 SirsteveHQ. All Rights Reserved.