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

free is the simplest among the various commands we'll encounter. It's used to display the current amount of physical and virtual RAM (RAM) being utilized by your system. By default, it outputs to standard out. You can run the free utility without any flags to get an idea about how much RAM is currently being used by your system.

Command

$ free

Output

total used free shared buff/cache available
Mem: 8021048 1320432 5689744 335556 1010872 6121932
Swap: 0 0 0

We can use the -h or --human flag to display the output in a human readable format.

Command

$ free -h

Output

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,600 GB of RAM. There are many other options that can be useful for printing the results in the format we want them printed. One more interesting one is the -k (kilobyte) and -M (megabyte) options.

$ free -h -s 5

The -s flag means “seconds”, so free will print out the RAM use every five seconds in this example. You can stop the program by pressing Ctrl+C.

Using vmstat Command

When we run the vmstat program, it will print out the free, buffed, and cached memory alongside the swapspace, CPU, IO,and sys info.

Command

$ vmstat -w

Output

--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

We're interested in the total memory usage so we need to use the -s (statistics) flag.

Command

$ vmstat -s

Output

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
76393 non-nice user cpu ticks
5 nice user cpu ticks
14122 system cpu ticks
337834 idle cpu ticks
1790 IO-wait cpu ticks
1463 IRQ cpu ticks
614 softirq cpu ticks
0 stolen cpu ticks
617253 pages paged in
310410 pages paged out
0 pages swapped in
0 pages swapped out
1426200 interrupts
4722588 CPU context switches
1625563254 boot time
1949 forks

The first line in the output contains the total number of bytes of physical memory available. We can get rid of everything except for the first line by using grep.

Command

$ vmstat -s | grep -i 'total memory' | sed 's/ *//'

Output

8021048 K total memory

Using top Command

Top is a tool that displays various statistics about your computer's resources. You can run it via the terminal (on Linux) or through the Windows GUI. To start it, type "top" into the terminal window.

Command

$ top

Output

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
top - 15:18:49 up 57 min, 1 user, load average: 2.61, 3.08, 2.03
Tasks: 138 total, 1 running, 137 sleeping, 0 stopped, 0 zombie
top - 15:18:58 up 58 min, 1 user, load average: 2.45, 3.02, 2.02
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 5324.5 avail Mem

At the top, you can see the system information for the computer. You can see that there is a total of 7833 MB of RAM installed.

dmidecode Utility

dmidecod is a utility that prints out useful info about your computer hardware. It extracts the relevant info from the DMI table (a part of the UEFI firmware). You can also use it to get some interesting info about your system such as its maximum supported RAM size.

Installing dmidecode

On some Linux distros, dmidecke isn't installed by default, so we're going to need to download and compile it first. We can get dmidecke from our distribution repository using an apt-get command.

To install the software for Ubuntu-based distros, we can use the command line tool "apt" −

# apt install dmidecode

To install Red Hat Enterprise Linux (RHEL), Fedora, or CentOS, we can use YUM −

# yum install dmidecode

On Arch-derived distros, just type 'pacman' −

# pacman -Sy dmidecode

Using dmidecode Utility

After installing the package, we can run the `dmidecode` commands from our terminal.

# dmidecode

To get access to the full list of available commands, we need to have root permissions. Once we run the dmidecode command, we’re going to be able to view detailed info about our system. However, we’re only concerned with one piece of info — the total amount of RAM installed on our computer.

We can use the -t type argument to specify which component we want to check. For example, if we wanted to see temperature readings from our CPU, we would run dmidecode -t 4.

We're interested in looking at the memory DMI types, which can be found by using the numbers 5, 6,16,17,18,19,20,21,22. However, we're interested in the one numbered 19, which will show us the RAM devices that are connected to the slots on our motherboards.

# dmidecode --type 19

Command

# dmidecode 3.3

Output

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

We only have a single RAM module with 8 GB of total memory capacity available in one of the RAM sockets.

Using /proc/meminfo Virtual File

The /proc folder is a special type of file system that contains virtual information about your computer. You can view these virtual folders through the command line, but you cannot modify them directly. They do not physically exist on your hard drive, so you cannot delete them or create new ones. However, you can open them and see what they contain. To get into the /proc folder, you must enter the following command: sudo su -c “cd /proc/”

Inside the /proc directory there is a special virtual folder called meminfofile which can be opened by typing cat into the terminal. It has information about our physical memory. Let's open it using cat −

$ cat /proc/meminfo

To illustrate the use of the “read” utility, let’s look at an example where we want to know the total RAM size of our system. First, we need to run the following commands −

Command

$ cat /proc/meminfo | head -n 3

Output

MemTotal: 8021048 kB
MemFree: 4542960 kB
MemAvailable: 5155668 kB

We've printed only the lines that interest us by specifying the line count (-n) with the help of the 'he' (or 'header') option. The sizes are printed out in kilobytes and will give us an idea of how much space we're currently taking up.

Suppose we're trying to write a bash script where we need to read the physical RAM from the /proc/memfile file and then use grep to get the total memory.

#!/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"

Next, we’ll save our script and make it executable.

Command

$ chmod +755 totalram.sh $ ./totalram.sh

Output

Total RAM: 8021048 kB

Conclusion

We looked at different command line options and tools that can help determine the physical RAM size of our Linux systems.

We discussed the minimal free space and its alternative vmstats commands. Then we examined the top program to see various system information. We then looked at the dmidescribe utility and how we can get the physical memory information from its DMI tables.

Afterwards, we learned what the /proc directory is and how we could read the mem info file to see if there was any memory available. We then learned about two graphical tools that we could use to monitor our system’s resources.

Updated on: 01-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements