How to Check Memory Size in Linux Operating System

Posted on

Introduction

In this article, the main focus is to show how to check memory size in Linux operating system. Basically, there are so many commands available or methods for achieving the task. One of the possible means is by using a certain command available in the operating system. The command is the ‘awk’ command by modifying the output where the information is available in a file with the name of ‘/proc/meminfo’. The modification of the ‘awk’ command is important in order to extract the necessary information from the file. According to the manual page of ‘awk’, awk is a tool or a command for pattern scanning and processing language. In this context, it will scan for the suitable pattern and process it. In order to extract the correct size information about the memory size in Linux operating system.

Checking the Memory Size in Linux Operating System

The following is the command modification from the simple one into the last format for checking the memory size in Linux operating system :

awk '( $1 == "MemTotal:")' /proc/meminfo

The output of the above command appears as follows :

In order to extract the correct size information about the memory size in Linux operating system, the following is the command :

user@hostname:~$ awk '( $1 == "MemTotal:")' /proc/meminfo 
MemTotal:       16182068 kB
user@hostname:~$ 

The above command is just a pattern scanning to find the match string of “MemTotal:”. In the end, the pattern exist and it will print the full line of it where there is the match of the pattern in that line. Following the modification of the above command exist in the following output The output of the above command appears as follows :

[root@user ~]# awk '( $1 == "MemTotal:") { print $2/1048576 " GB" }' /proc/meminfo 
7.63837
[root@user ~]# 

The command above is just a modification where it is containing another format. The additional format is ‘{ print $2/1048576 ” GB}”. By all means, it is a format for printing only the second column of the output. But before printing the string, divide the value with ‘1048576’. It is obvious since the goal is convert kB to GB. Since 1 GB is 1048576, just divide the value exist in the second column with 1048576. Finally, add another string with the characters of ‘GB’ to indicate that the number is in the GigaByte metric size.

Leave a Reply