The RAM usage on a system is good to know for a few reasons. Firstly, it can give you some insight into whether or not it’s necessary to upgrade the amount of memory inside your server or computer. If you see the memory utilization regularly nearing full capacity, it could indicate that your system needs an upgrade.
On the other hand, it can also help you track down problems on a system. A spike in memory usage can indicate an issue with a process running on the computer. In this tutorial for Linux administrators, we’ll go over a few methods to check and monitor the RAM usage on Linux.
In this tutorial you will learn:
- How to check RAM usage with free
- Monitoring ongoing RAM usage with free
- How does free work?
- How to check RAM usage with top
- How to check RAM usage with htop
Software Requirements and Conventions Used
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Ubuntu, Debian, CentOS, RHEL, Fedora |
Software | None |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
How to check RAM usage with free
The free
Linux command provides a very quick and easy way to see a system’s current memory utilization. The output requires a little knowledge to interpret, but we’ll cover that below. A few switches are also handy to know. Here’s what free
shows us on our test system:
# free total used free shared buff/cache available Mem: 2035476 627700 443420 19828 964356 1231164 Swap: 969960 0 969960
As you can see, it’s not the simplest thing to interpret. That’s chiefly because the output is given in kibibytes by default. The -h switch, which stands for “human readable”, helps us make more sense of the output:
# free -h total used free shared buff/cache available Mem: 1.9G 784M 145M 20M 1.0G 1.0G Swap: 947M 0B 947M
Now the values are much clearer, even with a brief glance. This output tells us that our system has about 2 GB of physical memory, and about 1 GB of swap memory. Let’s break down the details represented in all of these columns, since the terminology here gets a little confusing.
Total: This column is obvious – it shows how much RAM is physically installed in your system, as well as the size of the swap file.
Used: This column lists the amount of memory that is currently in use – but wait, that’s not quite as intuitive as it sounds. Just because memory is “in use” doesn’t necessarily mean that any process or application is actively utilizing it.
While the “used” column does represent RAM which is currently in use by the various programs on a system, it also adds in the RAM which the kernel is using for buffering and caching. This makes read and write operations more efficient, but the kernel will reallocate that memory if a process needs it.
The number in this column is the sum of total-free-buffers-cache.
Free: This column lists the amount of memory that is completely unutilized. There should ordinarily be a pretty small number here, since Linux uses most of the free RAM for buffers and caches, rather than letting it sit completely idle. As you can see in our example output above, our test machine has a measly 145 MB of memory that is totally free.
Shared: This column displays the amount of memory dedicated to tmpfs, “temporary file storage”. As the name implies, this file system stores temporary files to speed up operations on your computer. In Linux, tmpfs is represented as a mounted file system, though none of these files are actually written to disk – they are stored in RAM, hence the need for this column.
For the curious, a system’s tmpfs storage spaces can be observed with the df
command:
# df -h --type=tmpfs Filesystem Size Used Avail Use% Mounted on tmpfs 199M 1.4M 198M 1% /run tmpfs 994M 0 994M 0% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 994M 0 994M 0% /sys/fs/cgroup tmpfs 199M 36K 199M 1% /run/user/1000 tmpfs 199M 0 199M 0% /run/user/0
Buffer/Cache: This column contains the sum of the buffer and cache. Linux utilizes the buffer and cache to make read and write operations faster – it’s much quicker to read data from memory than from a hard disk. Most of the memory represented here can be reclaimed by processes whenever needed.
You can see these two columns separately by specifying the -w (wide) option:
# free -h -w total used free shared buffers cache available Mem: 1.9G 780M 82M 20M 109M 1.0G 1.0G Swap: 947M 0B 947M
Available: This column contains an estimation (an accurate one, but nonetheless an estimation) of memory that is available for use. The number in this column is a sum of the free column and cached RAM that is available for reallocation.
This is the column you should look to if you simply want to answer “how much free RAM does my system have available?” Likewise, to figure out how much RAM is currently in use (not considering buffer and cache), subtract the available amount from the total amount.
Monitoring ongoing RAM usage with free
When running the free command, it shows the current RAM utilization at that moment in time. But free also has some options for running continuously, in case you need to keep an eye on the usage for a while.
This is handy if you want to see how memory is impacted while performing certain tasks on your system, such as opening a resource intensive program.
The -s
(seconds) switch allows free to run continuously, issuing new output every specified number of seconds. For example, to run the free
command every 3 seconds:
# free -s 3
To stop free from running, just press Ctrl+C.
If you only want free to run a certain number of times, you can use the -c
(count option). For example, this command would run free
3 times, before exiting the program:
# free -c 3
With no -s
option, the count option issues new output every second. But feel free to combine both switches to get the exact behavior you want. For example, to make free
human readable and output memory usage every 5 seconds, 20 times:
# free -h -s 5 -c 20
How does free work?
It’s worth mentioning that the free command is really just a concise way to see information that’s already displayed somewhere else. If you’d like to go straight to the source, take a peek at the /proc/meminfo
pseudo-file:
# more -10 /proc/meminfo MemTotal: 2035476 kB MemFree: 95280 kB MemAvailable: 1036360 kB Buffers: 116180 kB Cached: 931872 kB SwapCached: 36 kB Active: 1146732 kB Inactive: 589208 kB Active(anon): 676400 kB Inactive(anon): 32892 kB --More--(0%)
How to check RAM usage with top
Although the free
command does a great job of showing us the overall RAM utilization on a system, it doesn’t tell us which tasks are consuming the memory. That’s where the top
command excels, displaying the memory utilization for every process on the system.
# top
Pay attention to the %MEM column:
To sort the programs in top by memory usage, press Shift+m while running top. This will allow you to see which processes are hogging the most memory at a quick glance, and you can continually monitor their usage.
Just press q to exit top.
How to check RAM usage with htop
How about a program that combines the best of both worlds? The htop
utility can show us overall RAM utilization in a clear way, continually update the statistics, as well as show us how much memory each process is using.
Unlike free
and top
, htop
is oftentimes not included on Linux by default. Here’s how to install it:
Ubuntu and Debian:
$ sudo apt-get install htop
CentOS and Red Hat:
# yum install htop
Fedora:
# dnf install htop
Once it’s installed, just type htop to run it.
# htop
RAM utilization across the whole system is displayed at the top of the screen. To sort processes by memory usage, press F6 and then select %MEM using your keyboard’s arrow keys.
Exit htop at any time by pressing q.
Conclusion
In this article, we saw how to check and monitor RAM utilization on a Linux system. We learned about multiple tools that can help us with the monitoring, and how to use those tools efficiently.
Using the various methods from this guide, you’ll always be able to ascertain your system’s RAM usage, including which processes are actively using it. This can clue you into system problems or help determine whether or not your system would benefit from a memory upgrade.