Introduction

The ability to check memory usage on Linux systems follows the popular UNIX philosophy “there’s more than one way to do it”. We have multiple commands to check and diagnose memory usage on Linux systems. The free command displays the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel. The vmstat command reports information about processes, memory, paging, block IO, traps, and CPU activity. We can use the top command to obtain a dynamic real-time view of a running system. The top command can display system summary information as well as a list of tasks currently being managed by the Linux kernel. If we are using a system that has a GUI interface then we could also use the gnome-system-monitor to view memory utilization. High memory usage can cause severe performance bottlenecks on systems if not monitored properly and kept within certain threshold limits. These threshold limits may vary depending on the available memory resources and the type of workloads residing on the system. A system low on memory can have several adverse implications like not being able to serve visitors accessing a website on a web server, the shutdown of network and other system services and at times even causing the system to hang followed by a subsequent system crash.

In this article, we will show you how to check the total amount of memory (physical and swap) available on your system, the amount of memory being used. Finally, we’ll demonstrate how we can view hardware information about the system RAM using the dmidecode command.

 

1 Using the free command
The free command is the most simple and easy to use command to check memory usage on Linux. Here is a quick example

[root@linuxnix ~]# free -h
total used free shared buff/cache available
Mem: 976M 266M 412M 40M 297M 491M
Swap: 2.0G 0B 2.0G

The -h option with the free command displays information in a more human readable format in a manner similar to the -h option available with the df command. We can also use the -m and -g options to display memory and swap information in terms of MBs and GBs respectively. Using the free command without any options will display the memory related information in KBs. Here, total 976M is the total memory available on the system which is approximately 1GB. The used column shows the amount of RAM that has been used by the system and is calculated as total – free – buffers – cache. The buff/cache is the sum of memory used by buffers (Memory used by kernel buffers) and cache (Memory used by the page cache and slabs). The available memory is the estimation of how much memory is available for starting new applications, without swapping. Unlike the data provided by the cache or free fields, this field takes into account page cache and also that not all reclaimable memory slabs will be reclaimed due to items being in use.

 

2 Using the /proc/meminfo file
The free command obtains it’s data from the /proc/meminfo file and fomrats the information in a more readable manner. We can view the contents of this file directly to obtain a more detailed view of how memory is being consumed on the system. The information in the /proc/meminfo is available in kB. To demonstrate, we’ll filter out the total and the available memory rows from the file as shown below

[root@linuxnix ~]# cat /proc/meminfo | egrep 'MemTotal|MemAvailable'
MemTotal: 999936 kB
MemAvailable: 536948 kB
[root@linuxnix ~]#

These correspond to the total and available columns of the free command that we discussed earlier.

 

3 Using the vmstat command
The vmstat command is used to display memory statistics but it provides a lot of additional information like paging, block IO, traps, and CPU activity. This command is generally used with the below syntax:

vmstat <delay in sec> <count>

The count is the number of times the vmstat command should be executed and the delay is the time interval in seconds between subsequent command invocations.
Let’s run the vmstat command once with a delay interval of 1 second as shown below:

[root@linuxnix ~]# vmstat 1 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
3 0 0 428600 1004 330032 0 0 151 20 105 231 1 1 97 1 0
[root@linuxnix ~]#

Under the memory section, the command displays the free memory along with the memory used for buffers and cache. The measurement units for the memory are kB. We can use the vmstat command with the -s option. This displays a table of various event counters and memory statistics and the output being displayed does not repeat. Let’s filter out the total, used and free memory from the output of the vmstat -s command as shown in the below example.

[root@linuxnix ~]# vmstat -s | egrep '(total|used|free) memory'
999936 K total memory
240440 K used memory
428460 K free memory
[root@linuxnix ~]#

 

4 Using the top command
The top program provides a dynamic real-time view of a running system. It can display system summary information like CPU, memory, and swap as well as a list of processes or threads currently being managed by the Linux kernel. The output of the top command is dynamic by default i.e. it changes continuously depending on the system activity. But we can use some options with the top command to prevent this behavior as shown below

[root@linuxnix ~]# top -b -n 1 | head -5
top - 16:55:51 up 32 min, 2 users, load average: 0.04, 0.04, 0.08
Tasks: 125 total, 1 running, 124 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.7 us, 1.0 sy, 0.0 ni, 97.7 id, 0.6 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 999936 total, 427432 free, 240788 used, 331716 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 535688 avail Mem

When used with the -b option the top command will execute in batch mode and run only for the number of iterations specified after the -n option. This behavior is helpful if we wish to use the output of the top command in some scripts. The above top command output shows information similar to the free command that we had seen earlier.

 

5 Using Gnome desktop
The “Gnome System Monitor” application enables you to display basic system information and monitor system processes, usage of system resources, and file systems. We can start System Monitor by visiting System menu > Choose Administration > System Monitor option. Or type the following command at the shell prompt:

# gnome-system-monitor

 

View RAM information using dmidecode command:
To find out hardware information about the installed RAM, use the demidecode command. It reports useful information about the installed RAM on the system.

[sahil@linuxnix:~] $ sudo dmidecode -t 17
# dmidecode 2.12
SMBIOS 2.7 present.

Handle 0x1100, DMI type 17, 34 bytes
Memory Device
Array Handle: 0x1000
Error Information Handle: Not Provided
Total Width: 72 bits
Data Width: 64 bits
Size: 32 GB
Form Factor: DIMM
Set: 1
Locator: DIMM_A1
Bank Locator: Not Specified
Type: DDR3
Type Detail: Synchronous LRDIMM
Speed: 1866 MHz
Manufacturer: 00AD04B300AD
Serial Number: 408F2973
Asset Tag: 01144722
Part Number: HMT84GL7AMR4C-RD
Rank: 4
Configured Clock Speed: 1600 MHz

The above dmidecode command will list out hardware details for each DIMM slot on the system. For slots which do not have the memory module inserted will have ‘no module inserted’ written against the size. From the sample output provided above, we can determine that the size of the memory module is 32GB of type DDR3 and has a speed of 1866 MHz.

 

Conclusion

In this article, we showed you five different methods of extracting and filtering memory related information on Linux systems and we also showed you how to view hardware information for the RAM installed on the system. We hope that you’ve found this article to be useful and we look forward towards your feedback.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he's gained over the years with the rest of the community.