Checking memory size linux

how to determine the available physical memory in linux

I’m trying to figure if my software running on linux suffers from memory leak . I’ve tried to measure the available physical memory as found in /proc/meminfo (see below) but could understand which field(s) represents the available memory and what is the relation between MemFree, Cached, Buffers, Active, Inactive .

cat /proc/meminfo MemTotal: 124128 kB MemFree: 62872 kB Buffers: 0 kB Cached: 15624 kB SwapCached: 0 kB Active: 38724 kB Inactive: 11148 kB SwapTotal: 0 kB SwapFree: 0 kB Dirty: 0 kB Writeback: 0 kB AnonPages: 34272 kB Mapped: 14640 kB Slab: 5564 kB SReclaimable: 424 kB SUnreclaim: 5140 kB PageTables: 504 kB NFS_Unstable: 0 kB Bounce: 0 kB WritebackTmp: 0 kB CommitLimit: 62064 kB Committed_AS: 57936 kB VmallocTotal: 655360 kB VmallocUsed: 1016 kB VmallocChunk: 654328 kB 

5 Answers 5

This is simpler command to check memory usage:

/proc/meminfo is for overall system memory information. /proc/[pid]/status has the memory usage info for an individual process. (it’s also in /proc/[pid]/stat in a more machine parseable format).

In particular, VmData (size of data segment) and VmStk (size of stack segments) are most likely of use to you. Or just use ps or top instead of trying to read the data directly yourself.

The other numbers are likely to just be confusing, because the overall system memory usage is complicated by shared memory, various kinds of buffers, etc.

Источник

Find Out the Total Physical Memory (RAM) on Linux

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

1. Overview

Sometimes, we might need to check for total memory size on a server running Linux or we might need to use memory stats in shell scripts. Fortunately, we have access to numerous tools that we can use to check for total physical memory. In this tutorial, we’re going to take different approaches to serve that purpose by using several useful commands and tools.

2. free Command

free is the simplest of all the commands we’ll see. It’s used to print the physical and swap memory usage — by default, it prints to standard output. The free command is one of the widely used commands to quickly check for RAM stats because it’s available on most Linux distributions. We can simply type the free command on our terminal without any flags:

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

As we can, the size of the physical memory is printed in bytes. However, we can easily print the output in a human-readable format using the -h or –human flag:

 total used free shared buff/cache available Mem: 7.6Gi 1.3Gi 5.4Gi 318Mi 985Mi 5.8Gi Swap: 0B 0B 0B

As can be seen in the output above, we have a total of 7.6 GiB of RAM. Moreover, there are lots of other options that can be used to print the output in the format we like such as –kilo, –mega, –Giga, and so on. One more interesting option is the -s option:

Читайте также:  Принтер canon mf3228 linux

The -s flag stands for seconds, so free will print the RAM usage every 5 seconds in this example. It’s especially useful if we want to monitor the RAM usage at a specified interval. We can easily terminate the process with Ctrl + C keyboard shortcut.

3. vmstat Command

Like the free command, vmstat (virtual memory statistics) is also available on most Linux distributions. By default, when we type the vmstat command, it will print the free, buffered, and cached memory alongside swap, CPU, IO, and system information:

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

The -w or –wide flag prints the output in a wide readable format. Since we’re interested in total RAM size, we’re going to add the -s or –stats flag:

 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 entry in the output is total physical RAM. Sometimes, we don’t need all the info, so we can use grep to extract only the desired entry:

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

4. top Command

top is basically a command-line task manager with a real-time output. It’s used to check the running processes in a clean table layout. It also shows us the CPU, RAM, and system information in general. We can fire it up by simply running the top command:

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, we can see the memory information beside the Mem field and observe that we have a total of 7833 MiB of total RAM.

5. dmidecode Utility

dmidecode is a helper tool that prints information about hardware components – such as hardware specifications, serial numbers, and BIOS versions – in a human-readable format. The hardware information is extracted from the DMI table. We can also use it to find interesting information about our system such as the maximum supported memory.

5.1. Installing dmidecode

dmidecode is not installed on some Linux distributions by default, so we’re going to install it first. We can install dmidecode directly from our distribution repository using a package manager.

Читайте также:  Linux run python file

For Ubuntu-based distributions, we can install the package through apt:

For RHEL, Fedora, and CentOS, we can use yum:

On Arch-based distributions, simply use pacman:

5.2. Using dmidecode

Once the installation is complete, we can run the dmidecode command from our terminal:

We need to have root privileges to run dmidecode. Once we run the command, we’ll be able to see detailed hardware information. It contains a lot of information related to our hardware components, but we’re only interested in our total memory size.

Fortunately, we can feed the –type option to the command for different components such as BIOS, memory, processor, temperatures, and so on. The –type option expects a number for DMI types.

In our case, we want to look at the memory DMI type, which can be accessed with the assigned numbers 5, 6, 16, 17, 18, 19, 20, 21, and 22. However, we’re interested in the number 19, which will print the RAM devices that are currently attached to the slots on our motherboard:

# dmidecode 3.3 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

As we can see, we only have a single RAM device with 8 GB of total size installed in one of the RAM slots.

6. /proc/meminfo Virtual File

The /proc directory is a special type of directory that contains virtual files. These virtual files are a way to take a peek at the Linux kernel, processes, and configurations. These files don’t actually exist on the disk and take no space, but they’re created when they are read. Access to some of the files requires root access.

There’s a special virtual file named meminfo, inside the /proc directory, that can be read with commands like cat and less. The meminfo file contains information about our physical memory size among other useful information. Let’s read the /proc/meminfo file using cat:

In the example above, we’ve used the cat command, which is a useful utility that can read from a file or standard input. Once the command is executed, it will read /proc/meminfo and present us with a long output regarding our physical memory. We’re only interested in the total RAM size, which appears in the top three lines of the output:

$ cat /proc/meminfo | head -n 3
MemTotal: 8021048 kB MemFree: 4542960 kB MemAvailable: 5155668 kB 

We printed only the lines that we’re interested in by using the head command. The -n argument provided to the head command is used to specify the number of lines to read. The sizes are printed in kilobytes and should give us an idea of what our total, free, and available physical memory is.

Let’s suppose we’re writing a Bash script where we need to process the physical RAM size. We can achieve that by reading the /proc/meminfo file first and then grep the total memory size:

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

Now, let’s save the script and make it executable:

$ chmod +755 totalram.sh $ ./totalram.sh Total RAM: 8021048 kB

7. Using GUI Task Managers

There are a variety of GUI task managers that we can use to check for CPU, RAM, and network usage in addition to managing processes. Let’s take a quick look at the most popular ones for Linux.

Читайте также:  Дистрибутивы линукс для разработчиков

7.1. GNOME System Monitor

GNOME System Monitor is the default task manager that ships with the GNOME desktop environment. However, if we’re running a different desktop environment, we can easily look for the gnome-system-monitor package on our distribution package repository. Once it’s installed, we can launch it from the command line:

gnome system monitor

Upon navigating to the Resources tab, we can see our total memory size among other stats.

7.2. KSysGuard

KSysGuard is the default task manager developed by the KDE community and comes with KDE by default. Similarly, if we’re using a different desktop environment, we can easily install it on our system by looking for the ksysguard package in the package repository. We can launch it with the ksysguard command once it’s installed:

ksysguard

Once ksysguard starts, we can head to the System Load tab and see our memory information along with a nice graph.

8. Conclusion

In this article, we looked at different commands and tools that can help us find out the physical memory size of our Linux system.

We covered the minimal free command and its alternative vmstat command. Then we used the top command to find out various system information. We also looked at the dmidecode utility and how we can extract the physical memory information from the DMI table.

Afterward, we learned what the /proc directory is and how we can read the /proc/meminfo file to check for total RAM size and use it in a bash script. Finally, we learned about a couple of graphical task managers and how we can use them to check for RAM usage and stats.

Источник

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.

Источник

Оцените статью
Adblock
detector