Several commands report on how much memory
is installed and being used on Linux systems. You can be deluged with
details or get a quick and easy answer, depending on the command you
use.
Kevin Stanchfield(CC BY 2.0)There are numerous ways to get information on the memory installed
on Linux systems and view how much of that memory is being used. Some
commands provide an overwhelming amount of detail, while others provide
succinct, though not necessarily easy-to-digest, answers. In this post,
we'll look at some of the more useful tools for checking on memory and
its usage.
Before we get into the details, however, let's review a
few details. Physical memory and virtual memory are not the same. The
latter includes disk space that configured to be used as swap. Swap may
include partitions set aside for this usage or files that are created to
add to the available swap space when creating a new partition may not
be practical. Some Linux commands provide information on both.
Swap
expands memory by providing disk space that can be used to house
inactive pages in memory that are moved to disk when physical memory
fills up.
One file that plays a role in memory management is /proc/kcore.
This file looks like a normal (though extremely large) file, but it
does not occupy disk space at all. Instead, it is a virtual file like
all of the files in /proc.
$ ls -l /proc/kcore
-r--------. 1 root root 140737477881856 Jan 28 12:59 /proc/kcore
Interestingly, the two systems queried below do not have
the same amount of memory installed, yet the size of /proc/kcore is the
same on both. The first of these two systems has 4 GB of memory
installed; the second has 6 GB.
system1$ ls -l /proc/kcore
-r--------. 1 root root 140737477881856 Jan 28 12:59 /proc/kcore
system2$ ls -l /proc/kcore
-r-------- 1 root root 140737477881856 Feb 5 13:00 /proc/kcore
Explanations that claim the size of this file represents the
amount of available virtual memory (maybe plus 4K) don't hold much
weight. This number would suggest that the virtual memory on these
systems is 128 terabytes! That number seems to represent instead how
much memory a 64-bit systems might be capable of addressing — not how
much is available on the system. Calculations of what 128 terabytes and
that number, plus 4K would look like are fairly easy to make on the
command line:
Another and more human-friendly command for examining memory is the free command. It gives you an easy-to-understand report on memory.
$ free
total used free shared buff/cache available
Mem: 6102476 812244 4090752 13112 1199480 4984140
Swap: 2097148 0 2097148
With the -g option, free reports the values in gigabytes.
$ free -g
total used free shared buff/cache available
Mem: 5 0 3 0 1 4
Swap: 1 0 1
With the -t option, free shows the same values
as it does with no options (don't confuse -t with terabytes!) but by
adding a total line at the bottom of its output.
$ free -t
total used free shared buff/cache available
Mem: 6102476 812408 4090612 13112 1199456 4983984
Swap: 2097148 0 2097148
Total: 8199624 812408 6187760
And, of course, you can choose to use both options.
$ free -tg
total used free shared buff/cache available
Mem: 5 0 3 0 1 4
Swap: 1 0 1
Total: 7 0 5
You might be disappointed in this report if you're trying to
answer the question "How much RAM is installed on this system?" This is
the same system shown in the example above that was described as having
6GB of RAM. That doesn't mean this report is wrong, but that it's the
system's view of the memory it has at its disposal.
The free command also provides an option to update the display every X seconds (10 in the example below).
$ free -s 10
total used free shared buff/cache available
Mem: 6102476 812280 4090704 13112 1199492 4984108
Swap: 2097148 0 2097148
total used free shared buff/cache available
Mem: 6102476 812260 4090712 13112 1199504 4984120
Swap: 2097148 0 2097148
With -l, the free command provides high and low memory usage.
$ free -l
total used free shared buff/cache available
Mem: 6102476 812376 4090588 13112 1199512 4984000
Low: 6102476 2011888 4090588
High: 0 0 0
Swap: 2097148 0 2097148
Another option for looking at memory is the /proc/meminfo
file. Like /proc/kcore, this is a virtual file and one that gives a
useful report showing how much memory is installed, free and available.
Clearly, free and available do not represent the same thing. MemFree
seems to represent unused RAM. MemAvailable is an estimate of how much
memory is available for starting new applications.
DirectMap4k represents the amount of memory being mapped to
standard 4k pages, while DirectMap2M shows the amount of memory being
mapped to 2MB pages.
The getconf command is one that will provide quite a bit more information than most of us want to contemplate.
Pare that output down to something specific with a command like
the one shown below, and you'll get the same kind of information
provided by some of the commands above.
$ getconf -a | grep PAGES | awk 'BEGIN {total = 1} {if (NR == 1 || NR == 3) total *=$NF} END {print total / 1024" kB"}'
6102476 kB
That command calculates memory by multiplying the values in the first and last lines of output like this:
Calculating that independently, we can see how that value is derived.
$ expr 4096 \* 1525619 / 1024
6102476
Clearly that's one of those commands that deserves to be turned into an alias!
Another command with very digestible output is top. In the first five lines of top's output, you'll see some numbers that show how memory is being used.
And finally a command that will answer the question "So, how much RAM is installed on this system?" in a succinct fashion:
$ sudo dmidecode -t 17 | grep "Size.*MB" | awk '{s+=$2} END {print s / 1024 "GB"}'
6GB
Depending on how much detail you want to see, Linux systems
provide a lot of options for seeing how much memory is installed on your
systems and how much is used and available.
No comments:
Post a Comment