Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to find total physical memory (RAM) size on Linux?
We can often get away without checking for total memory usage on servers running Linux. However, sometimes we may want to know the total memory available on our servers. Luckily, there are various ways to accomplish this task. In this tutorial, I'll show you some of them.
Using free Command
The free command is the simplest among the various commands we'll encounter. It displays the current amount of physical and virtual RAM being utilized by your system. You can run the free utility without any flags to get an idea about how much RAM is currently being used by your system.
Basic Usage
$ free
total used free shared buff/cache available
Mem: 8021048 1320432 5689744 335556 1010872 6121932
Swap: 0 0 0
For better readability, use the -h flag to display output in human-readable format ?
$ free -h
total used free shared buff/cache available
Mem: 7.6Gi 1.3Gi 5.4Gi 318Mi 985Mi 5.8Gi
Swap: 0B 0B 0B
We have a total of 7.6 GB of RAM. Other useful options include -k (kilobytes), -m (megabytes), and -s for continuous monitoring ?
$ free -h -s 5
The -s flag means "seconds", so free will print out the RAM usage every five seconds. You can stop the program by pressing Ctrl+C.
Using vmstat Command
The vmstat program prints out free, buffered, and cached memory alongside swap space, CPU, I/O, and system information.
Basic vmstat Output
$ vmstat -w
--procs-- -----------------------memory---------------------- ---swap-- -----io---- -system-- --------cpu--------
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 5352900 56816 1053708 0 0 162 73 328 1087 18 4 78 0 0
For detailed memory statistics, use the -s flag ?
$ vmstat -s
8021048 K total memory
1564516 K used memory
305336 K active memory
1962636 K inactive memory
5391588 K free memory
58224 K buffer memory
1006720 K swap cache
0 K total swap
0 K used swap
0 K free swap
To extract only the total memory information ?
$ vmstat -s | grep -i 'total memory'
8021048 K total memory
Using top Command
The top command displays real-time statistics about your computer's resources, including memory usage.
$ top
top - 15:18:13 up 57 min, 1 user, load average: 3.40, 3.26, 2.04 Tasks: 138 total, 1 running, 137 sleeping, 0 stopped, 0 zombie %Cpu(s): 17.2 us, 3.6 sy, 0.0 ni, 77.5 id, 0.3 wa, 0.9 hi, 0.5 si, 0.0 st MiB Mem : 7833.1 total, 4665.9 free, 1782.3 used, 1384.8 buff/cache MiB Swap: 0.0 total, 0.0 free, 0.0 used. 5324.5 avail Mem
The memory information appears in the fourth line, showing a total of 7833.1 MB of RAM installed.
Using dmidecode Utility
dmidecode is a utility that extracts hardware information from the DMI table (part of UEFI firmware). It can provide detailed information about your system's maximum supported RAM size and installed memory modules.
Installing dmidecode
On Ubuntu-based distributions ?
# apt install dmidecode
On RHEL, Fedora, or CentOS ?
# yum install dmidecode
On Arch-based distributions ?
# pacman -S dmidecode
Using dmidecode for Memory Information
To view memory array information, use DMI type 19 ?
# dmidecode --type 19
Getting SMBIOS data from sysfs.
SMBIOS 3.0.0 present.
Handle 0x0049, DMI type 19, 31 bytes
Memory Array Mapped Address
Starting Address: 0x00000000000
Ending Address: 0x001FFFFFFFF
Range Size: 8 GB
Physical Array Handle: 0x0044
Partition Width: 1
This shows we have a single 8 GB RAM module installed.
Using /proc/meminfo Virtual File
The /proc directory contains virtual files with system information. The meminfo file provides detailed memory statistics.
$ cat /proc/meminfo | head -n 3
MemTotal: 8021048 kB MemFree: 4542960 kB MemAvailable: 5155668 kB
Bash Script Example
Here's a simple bash script to extract total RAM information ?
#!/bin/bash
total_ram() {
local totalram=$(cat /proc/meminfo | grep -i 'memtotal' | grep -o '[[:digit:]]*')
echo $totalram
}
ram_size=$(total_ram)
echo "Total RAM: $ram_size kB"
Make it executable and run ?
$ chmod +x totalram.sh $ ./totalram.sh
Total RAM: 8021048 kB
Comparison of Methods
| Command | Speed | Detail Level | Human Readable | Root Required |
|---|---|---|---|---|
| free | Fast | Basic | Yes (-h flag) | No |
| vmstat | Fast | Medium | No | No |
| top | Medium | High | Yes | No |
| dmidecode | Slow | Very High | Yes | Yes |
| /proc/meminfo | Fast | High | No | No |
Conclusion
Linux provides multiple methods to check total physical memory, each serving different needs. The free command is quickest for basic information, while dmidecode offers detailed hardware specifications. Choose the method that best fits your requirements and access level.
