How to Get the Total Physical Memory Information of a Machine from /proc/meminfo

Posted on


This article has a detail review on the usage of the information in /proc/meminfo file. It is an information about the memory in a machine. In order to access the content of the file, just print it by a certain command or tool using ‘cat’ command. But in order to get the information from the content available in the file, there will be a need for further processing. In order to process the content into a suitable information, just modify the command execution. So that command modification will print the necessary information.


Extract Physical Memory Information from /proc/meminfo using cat command

The following is an example of the command execution using the normal ‘cat’ command to print the content of the ‘/proc/meminfo’ file :

cat /proc/meminfo

The command execution of the above command pattern will appear as follows :

user@hostname:~$ cat /proc/meminfo 
MemTotal:       16182160 kB
MemFree:          245556 kB
MemAvailable:    1378644 kB
Buffers:           78112 kB
Cached:          1794840 kB
SwapCached:        35172 kB
Active:          7526912 kB
Inactive:        2779904 kB
Active(anon):    7026936 kB
Inactive(anon):  1969220 kB
Active(file):     499976 kB
Inactive(file):   810684 kB
Unevictable:       13596 kB
Mlocked:           13596 kB
SwapTotal:       8142844 kB
SwapFree:        2750116 kB
Dirty:              2940 kB
Writeback:             0 kB
AnonPages:       8413252 kB
Mapped:          5111560 kB
Shmem:            549892 kB
Slab:             505828 kB
SReclaimable:     154228 kB
SUnreclaim:       351600 kB
KernelStack:       34208 kB
PageTables:       175472 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:    16233924 kB
Committed_AS:   36818292 kB
VmallocTotal:   34359738367 kB
VmallocUsed:           0 kB
VmallocChunk:          0 kB
HardwareCorrupted:     0 kB
AnonHugePages:      6144 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
CmaTotal:              0 kB
CmaFree:               0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:     6953988 kB
DirectMap2M:     9578496 kB
DirectMap1G:     1048576 kB

Filtering Information from /proc/meminfo using awk command to get Physical Memory size

It is quite difficult to read the information from the output of the above command execution. In order to make the information a little bit simple, modify the command for further execution :

cat /proc/meminfo | awk '/MemTotal/'

The output of the above command execution exist as in the following below :

user@hostname:~$ cat /proc/meminfo | awk '/MemTotal/'
MemTotal:       16182160 kB

The only important information exist in the second column. So, just print the information in the second column. Below is the command modification for printing only the second column :

user@hostname:~$ cat /proc/meminfo | awk '/MemTotal/ { printf $2 "\n" }'

Furthermore, just change the metric size of the output command. Change it into Gigabyte instead of kilobyte. Since 1 Gigabyte is 1024 Megabyte and 1 Megabyte is 1024 kilibyte, the following is the complete command execution with the output of it :

user@hostname:~$ cat /proc/meminfo | awk '/MemTotal/ { printf $2 / (1024*1024) " GB\n"}'
15.4324 GB

Finally, the last one is just to make the output in a neat form of double digit decimal. In order to format it in that kind of form, just modify the command into the following command :

user@hostname:~$ cat /proc/meminfo | awk '/MemTotal/ { printf "%0.2f GB\n", $2 / (1024*1024) }'
15.43 GB

The additional attribute for printf command, it is ‘%0.2f” where it will format the printing output into a float type format with two decimal digit in the end.

Leave a Reply