- How can I find out the total physical memory (RAM) of my linux box suitable to be parsed by a shell script?
- How to check memory size in Linux
- How to check memory size in Linux (command line)
- How to check memory size in Linux (GUI method)
- Closing Thoughts
- Related Linux Tutorials:
- How to check RAM size?
- 4 ways to check the size of physical memory (RAM) in Linux
- 1. Using free command
- 2. Using /proc/meminfo file
- 3. Using top command
- 4. Using vmstat
How can I find out the total physical memory (RAM) of my linux box suitable to be parsed by a shell script?
I’m typing a shell script to find out the total physical memory in some RHEL linux boxes. First of all I want to stress that I’m interested in the total physical memory recognized by kernel, not just the available memory. Therefore, please, avoid answers suggesting to read /proc/meminfo or to use the free, top or sar commands — In all these cases, their «total memory» values mean «available memory» ones. The first thought was to read the boot kernel messages:
Memory: 61861540k/63438844k available (2577k kernel code, 1042516k reserved, 1305k data, 212k init)
But in some linux boxes, due to the use of EMC2’s PowerPath software and its flooding boot messages in the kernel startup, that useful boot kernel message is not available, not even in the /var/log/dmesg file. The second option was the dmidecode command (I’m warned against the possible mismatch of kernel recognized RAM and real RAM due to the limitations of some older kernels and architectures). The option —memory simplifies the script but I realized that older releases of that command has no —memory option. My last chance was the getconf command. It reports the memory page size, but not the total number of physical pages — the _PHYS_PAGES system variable seems to be the available physical pages, not the total physical pages.
# getconf -a | grep PAGES PAGESIZE 4096 _AVPHYS_PAGES 1049978 _PHYS_PAGES 15466409
My question: Is there another way to get the total amount of physical memory, suitable to be parsed by a shell script?
How to check memory size in Linux
If you want to know the memory size of your Linux system, you will be relieved to know that it is not necessary to crack open the PC or boot into the BIOS screen to get your information. Linux allows us to check our memory size, and other relevant information such as spare slots and RAM speed, etc, all from within the operating system. We will go over some of these hardware checking methods below.
In this tutorial, you will learn how to check the physical memory (RAM) size that is installed on your computer. We will go over several command line and GUI methods below, which should work for any Linux distro that you may be using.
In this tutorial you will learn:
- How to check memory size via command line
- How to check memory size via GNOME System Monitor
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Any Linux distro |
Software | free, lshw, GNOME System Monitor |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions | # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command $ – requires given linux commands to be executed as a regular non-privileged user |
How to check memory size in Linux (command line)
- The free command will give us information about current RAM usage, and break down how it is being utilized across our system. But it also gives us a clear indicator of exactly how much memory we have installed in our system. For best results, we recommend using the -g , -h , and -t options. This will tell free to display RAM amounts in Gigabytes, make the amounts human readable, and give us the totals, respectively.
You can ignore the swap memory here since that is not physically installed RAM. The line you want to pay attention to is the very first one. In our example, we have 3.8 GB of RAM installed.
The next method is not actually a command, but a file. The meminfo file contains a lot of information about the RAM installed in our system, and is what many other commands query in order to display their data. You can just use cat or less to view the file:
The very first line displays what you need to see. But you may also be interested in the plethora of other information displayed by this file.
The lshw command can detect the number of RAM slots used, speed, and size. You must execute this command with root permissions.
How to check memory size in Linux (GUI method)
There are many GUI applications which can be used to check the amount of physically installed memory. But the one that we will be going over in this section is called GNOME System Monitor. If you are already using the GNOME desktop environment, then GNOME System Monitor should already be installed by default. But if you do not already have the application, follow the installation instructions below:
You can use the appropriate command below to install the GNOME System Monitor with your system’s package manager.
To install the GNOME System Monitor on Ubuntu, Debian, and Linux Mint:
$ sudo apt install gnome-system-monitor
To install the GNOME System Monitor on Fedora, CentOS, AlmaLinux, and Red Hat:
$ sudo dnf install gnome-system-monitor
To install the GNOME System Monitor on Arch Linux and Manjaro:
$ sudo pacman -S gnome-system-monitor
- After installation, search for and open the GNOME System Monitor application. On the GNOME desktop environment, this should be inside of the Activites menu.
- With the application open, click on the ‘Resources’ tab at the top of the menu.
- You will see the total amount of physically installed memory where indicated in the screenshot below:
Closing Thoughts
In this tutorial, we saw how to check the memory size on a Linux system. These commands and the GUI program should help you get to know your hardware without forcing you to crack open the PC and examining individual components. This ends up saving a lot of time and gives us a concise output of what RAM is installed in our system.
Related Linux Tutorials:
Comments and Discussions
How to check RAM size?
-m , —mebi Display the amount of memory in mebibytes.
—mega Display the amount of memory in megabytes. Implies —si .
free -h will output it in human-readable output (usually GiB: Gibibytes—base 1024). But, since RAM is usually sold in GB (Gigabytes—base 1000), NOT GiB (Gibibytes), free -h —si is usually better, as @Jollywatt says, to output it in Gigabytes instead.
Open a terminal ( CTRL + ALT + T ).
Run following command to see RAM information in KB (1 KB is equal to 1024 bytes).
Run following command to see RAM information in MB (1 MB is equal to 1024 KB).
Run following command to see RAM information in GB (1 GB is equal to 1024 MB).
Or you can run following command to see more information about the same:
free -h seems most helpful to me. «all output fields automatically scaled to shortest three digit unit»
Click on the power/gear icon (System Menu) in the top right corner of the screen and choose About This Computer. You will see the total available memory in GiB. Multiply the value by 1024 to get the size in MiB.
This value (and the value shown as Total in output of free -m on the console), is total physical RAM size, minus the amount assigned to the internal GPU, if your computer has one.
To see the total amount of physical RAM installed, you can run sudo lshw -c memory which will show you each individual bank of RAM you have installed, as well as the total size for the System Memory. This will likely presented as GiB value, which you can again multiply by 1024 to get the MiB value.
4 ways to check the size of physical memory (RAM) in Linux
In this article we will see basic commands to check physical memory on a server in Linux. Many beginners struggle with knowing their system well in context to resources like CPU, Memory, disks, etc. So I decided to write this small article pinpointing commands to check RAM on the Linux server. These commands will work in different flavors of Linux like Red Hat, CentOS, Suse, Ubuntu, Fedora, Debian, etc.
Without much delay lets dive into commands –
1. Using free command
The first command is free. This is the simplest command to check your physical memory. This command is mainly used for checking RAM and SWAP on the system. Using different switch you can change the byte-format of output. Like -b for bytes, -k for kilobytes, -m for megabytes and -g for gigabytes.
Check row with Mem: and number against it. That’s the physical RAM of your server.
root@kerneltalks # free -b total used free shared buffers cached Mem: 135208493056 1247084544 133961408512 0 175325184 191807488 -/+ buffers/cache: 879951872 134328541184 Swap: 17174347776 0 17174347776 root@kerneltalks # free -k total used free shared buffers cached Mem: 132039544 1218368 130821176 0 171216 187316 -/+ buffers/cache: 859836 131179708 Swap: 16771824 0 16771824 root@kerneltalks # free -m total used free shared buffers cached Mem: 128944 1189 127754 0 167 182 -/+ buffers/cache: 839 128105 Swap: 16378 0 16378 root@kerneltalks # free -g total used free shared buffers cached Mem: 125 1 124 0 0 0 -/+ buffers/cache: 0 125 Swap: 15 0 15
In the above output you can see the system is installed with 125GB of physical RAM (observe highlighted rows). By using a different switch -b , -k , -m and -g output changed numbers according to selected byte-format.
2. Using /proc/meminfo file
Another way is to read memory info from the proc filesystem. /proc/meminfo is the file you should read to get detailed information about memory. The very first line or line starts with MemTotal is your total physical memory on the server.
root@kerneltalks # cat /proc/meminfo |grep MemTotal MemTotal: 132039544 kB
As you can see from output, memory is displayed in kilobytes.
3. Using top command
The famous top command also lists physical memory information in a very clear way. In the upper section of the top command output lies the CPU, Memory, and SWAP information.
root@kerneltalks # top top - 16:03:41 up 89 days, 3:43, 1 user, load average: 0.00, 0.01, 0.05 Tasks: 141 total, 1 running, 140 sleeping, 0 stopped, 0 zombie Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 132039544k total, 1218336k used, 130821208k free, 171224k buffers Swap: 16771824k total, 0k used, 16771824k free, 187420k cached
I clipped the above section of the top command output in the above example. Check second last line saying Mem: (highlighted row). This shows physical memory in kilobytes. You can see the total, used, and free portions of it. Total is your actual RAM installed on the server.
4. Using vmstat
Another way is to use vmstat (virtual memory stats) command with -s switch. This will list memory in detail with the first-line being total memory on the server.
# vmstat -s 132039544 total memory 1218692 used memory 181732 active memory ----output trimmed----
Memory is displayed in kilobytes by default. The very first line shows you total memory on the server.