Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

Any process running in our machine requires memory. And the memory requirement varies from process to process.

At times we need to monitor the memory usage. Sometimes it would be for debugging a newly created application. Or it would be for finding the overall memory usage of a server. In any case, the first step is to log the memory usage.

In this tutorial, we’ll check the different ways we can monitor memory consumption.

2. Using the free Command

The very first command that comes to our mind for checking memory usage is the free command. As we know, this command prints the used, free, buffered, and available memory.

Usage is:

free [options]

The free command works in two modes. One is to print the memory usage once and exit, which is the default behavior. Another is to print it continuously. Since we aim to monitor the memory usage, we’ll use the latter mode.

Let’s take a look at it:

$ free -s 1 -h -c 3
              total        used        free      shared  buff/cache   available
Mem:           7.7G        2.9G        2.7G        175M        2.2G        4.4G
Swap:            0B          0B          0B

              total        used        free      shared  buff/cache   available
Mem:           7.7G        2.9G        2.7G        175M        2.2G        4.4G
Swap:            0B          0B          0B

              total        used        free      shared  buff/cache   available
Mem:           7.7G        2.9G        2.7G        176M        2.2G        4.4G
Swap:            0B          0B          0B

The above command printed the memory usage every second three times.

And from the results, we can see that the total RAM is 7.7GB. Of that, 2.9GB has been used. And there is a free RAM of 2.7GB.

Let’s check the options we’ve used:

  • -s 1: set the delay in printing the result; here, we’ve used one second
  • -h: print result in human-readable format
  • -c 3: set the count to print the result

As we can see, these options come in handy when we need to monitor memory usage for a long time. Furthermore, we may redirect the output to a file. That way, we can refer to it later when we need it.

3. Using the top Command

The top command is also typical among Linux users as the free command. But, this command gives more information regarding the CPU, memory, and processes.

By default, running this command starts the command in an interactive mode. And it prints all the running processes and their usage of various system resources. Users can interact with any process from this screen.

In order to monitor the memory consumption continuously, we’ve to use the batch mode option provided by the top command.

Let’s take a look at it:

$ top -b -d1 -o +%MEM > memory.log

The above command redirects the result to the memory.log file. And it’ll have the result of the top command every second.

Let’s check the different options we’ve used here:

  • -b: enable batch mode and don’t accept input from the user
  • -d1: set a delay of 1 second between each result
  • -o +%MEM: override the default sort option to use the %MEM column value; the + sign is used to sort in descending order

This command will log details of all processes to the memory.log file. With this, we can also see the process which uses more memory.

On the other hand, if we just wanted to log the overall memory consumption, we can use the grep command to filter just that.

Let’s see how that works:

$ top -b -d1 -o +%MEM | grep -A1 'KiB Mem'
KiB Mem :  8072956 total,  2813224 free,  3001308 used,  2258424 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  4596252 avail Mem 
--
KiB Mem :  8072956 total,  2813224 free,  3001308 used,  2258424 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  4596252 avail Mem 
--
KiB Mem :  8072956 total,  2813224 free,  3001308 used,  2258424 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  4596252 avail Mem 

Here, we’ve used the same top command we tried before. In addition to that, we used the grep command to filter the memory-related information. As a result, we can see that it has printed the main memory and swap memory usage. We need to press ctrl-c to quit out of the command.

In order to show the swap memory usage, we used the -A1 option in the grep command to print one line below the matching line.

4. Using the sar Command

Let’s look at another command used to print the system information. The sar command prints the system activity information. It also includes an option to print the usage statistics continuously.

Unlike the other two commands we saw earlier, this prints more information about memory usage.

The general usage is:

sar [ options ] [ <interval> [ <count> ] ]

Let’s look at a sample usage:

$ sar -o memory.log -r 1 3 --human
Linux 4.15.0-76-generic (slave-node) 	04/21/22 	_x86_64_	(4 CPU)

15:00:23    kbmemfree   kbavail kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty
15:00:24         2.7G      4.4G      5.0G     65.2%    779.4M   1012.5M      7.5G     97.4%      3.8G    605.7M     84.0k
15:00:25         2.7G      4.4G      5.0G     65.2%    779.4M   1012.5M      7.5G     97.4%      3.8G    605.8M    120.0k
15:00:26         2.7G      4.4G      5.0G     65.2%    779.4M   1012.6M      7.5G     97.4%      3.8G    605.9M    156.0k
Average:         2.7G      4.4G      5.0G     65.2%    779.4M   1012.5M      7.5G     97.4%      3.8G    605.8M    120.0k

The above results show that it has printed the memory details thrice with a one-second delay. It has also printed the average for that session.

In order to get this result, we’ve used the below options:

  • -o: using this flag captures the output to the filename specified in a binary format
  • -r: to print memory statistics; by default, it prints CPU details
  • 1: we’ve given 1 second as the interval
  • 3: to print the results three times
  • –human: to show the result in human-readable format

Once we run this command, we will create a new file named memory.log in the current directory. Since this file is in binary format, we’ll have to use the sar command with the -f option to read it.

Even though we used the -o option, the command will also print the same information to the screen. In case we want to run this as a daemon, we can redirect this output to /dev/null.

5. Using the vmstat Command

The vmstat command is used to display information about memory, CPU, disk, and processes. Similar to the sar command, the vmstat command also monitors the system resources. This also gives a lot of information regarding their state.

Usage is:

vmstat [options] [delay [count]]

Let’s check how we can run this command:

$ vmstat 1 3 -a -S M -w -t
procs -----------------------memory---------------------- ---swap-- -----io---- -system-- --------cpu-------- -----timestamp-----
 r  b         swpd         free        inact       active   si   so    bi    bo   in   cs  us  sy  id  wa  st                 IST
 0  0            0         2747          605         3855    0    0     1    20    1    0   1   0  98   1   0 2022-04-21 15:00:35
 0  0            0         2747          605         3855    0    0     0     0  222  428   1   0  99   0   0 2022-04-21 15:00:36
 0  0            0         2747          605         3855    0    0     0     0  138  341   1   0 100   0   0 2022-04-21 15:00:37

From the results, we can see that the memory usage and other stats are printed thrice.

Let’s look at the different options we’ve given:

  • 1: delay in printing the statistics
  • 3: number of times we wanted to print the statistics
  • -a: print the active and inactive memory information; by default, it prints the buffer and cache information
  • -S M: tells to print the statistics in MB
  • -w: set the wide mode to accommodate the large values
  • -t: to print the timestamp in the result

6. The /proc/meminfo File

Finally, let’s check the meminfo file in the proc file system. This file holds all the memory-related information. In fact, the free and the top commands use this file to gather the memory usage information.

We can cat this file to see its contents. But that would give us the information one time. So we can write a one-line script to print the memory parameters continuously.

Let’s check that:

$ while true; do cat /proc/meminfo | grep 'MemTotal\|MemFree'; echo; sleep 1; done
MemTotal:        8072956 kB
MemFree:         2813452 kB

MemTotal:        8072956 kB
MemFree:         2812972 kB

MemTotal:        8072956 kB
MemFree:         2812576 kB

Here, we’ve got an infinite while loop that cat the /proc/meminfo file. Then it is fed to the grep command to filter for MemTotal and MemFree fields. We can add more fields as we need to the grep command. Then we echo a blank line as a separator. Finally, we sleep for a second and repeat the whole process until the user press ctrl-c.

7. Conclusion

In this tutorial, we’ve seen the different ways to monitor memory usage. We may choose the one that fits our requirements.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!