How to understand memory usage on a linux server

269ac00f765508cb74b25d006dbf5bb2

Written by Dennis Nind

Last published at: February 8th, 2019

Overview:

Sometimes you'll notice your server reports little or no "Free Memory," however, this might not mean that your server is really low on memory.

To see your server's memory, first, log into the WHM interface along with your root user data, then visit Home >> Server standing >> Server data and so scroll right down to "Current Memory Usage" and you may see one thing like this for the server's output:

Code:

# free -m
              total        used        free      shared  buff/cache   available
Mem:           1838         906         126         105         804         651
Swap:             0           0           0 

In some cases, this can cause confusion or falsely indicate the system is low on memory. In the case of Linux, the section that you will want to look at in the server is the available column because Linux will allocate your free memory and use it for disk caching. This will cause different values to be reported between the used memory column and the available column.

Looking at each column, we can see that the server has a total of 1838 MiB of memory, the server is currently using 906 MiB and then 126 MiB free. For the actual free memory, we will look at the available memory which is 651 MiB.


What is disk caching?

Disk caching is managed through the Linux kernel, which stores common requests and processes in memory so that the server's IO wait is lower. This helps applications run smoother, and if more memory is needed for other processes, the kernel will release the memory from the disk cache. If the memory is needed for other processes, the system will release the memory from the disk caching so that it may be used normally.


FAQ:


Q: Can Disk Caching be disabled?

A: No, since this is a kernel level operation there isn't a way to disable disk caching.


Q: What is the difference between MB and MiB?

A: MB stands for megabyte and is 10 to the power of 6 bytes. MiB stands for mebibyte and is 2 to the power of 20 bytes.


1 MB = 1,000,000 bytes

1 MiB = 1,048,576 bytes

MB=220 and MiB=106


Making sense of it all

Let’s take a look at a pre-3.3.10 example.

$ free -m
             total       used       free     shared    buffers     cached
Mem:           925        890         34         27         17        777
-/+ buffers/cache:         95        830
Swap:           99         67         32

The first row contains the statistics for the system’s physical memory. Let’s break down the columns one by one.

  • total => The total amount of memory. On this machine (a Raspberry Pi 3 Model B), we can observe that 925MB of physical memory are present.
  • used => The amount of memory currently in use. (Note: this value is calculated differently in 3.3.10, as we’ll cover.)
  • free => The amount of memory that is not in use.
  • shared => The amount of memory that is shared between processes.
  • buffers => The amount of memory that is used for kernel buffers. Kernel buffers are generally utilized for temporary I/O storage.
  • cached => The amount of memory that is cached by the system. This cached data is used to improve system performance.

According to the output from free, ~890MB of the RAM are “used”, while ~34 MB are “free”. You might think this is indicative of a system that is short on memory, but that is not so. Memory that is being used for kernel buffers or the disk cache counts as “used” memory and subtracts from the amount of “free” memory. Roughly speaking (more on this later), the ~777MB in use by the disk cache is not strictly required, such that the memory could be used by an application that needs it.

Disk caching on Linux is designed to be unobtrusive, such that performance may be improved without taking away memory from running processes. Generally speaking, your “free” memory will be low, as Linux will use memory for disk caching to improve system performance.

This brings us to the second row. In the second row, the amounts in the buffers and cache are subtracted from the used memory, and this difference is added to the free memory. This row serves as an estimate of the amount of memory that is unavailable (used) vs available (free). This rough estimate from adding to and subtracting from the amount of memory in the buffers and disk cache, as well as the “used” amount reported, have been updated in the more recent releases of procps-ng (>= 3.3.10), as we will show below.

The final row relates to the allocated swap space. Swap space is space on disk that is generally utilized when there is not enough physical memory present. It acts as a temporary location for swapping in and out pages of memory as needed. (A page is a chunk of memory that is the primary unit of memory in which the kernel and CPU deals. Many posts could go into this topic…)


What changed in the newer versions of free?

Let’s take a look.

$ free -m
              total        used        free      shared  buff/cache   available
Mem:          31953        1789        5818        1619       24345       27443
Swap:         16063         853       15210

In the recent versions of free, 'used' does not include buffers and cached data, as you can see from the relevant commit.

A new column “available” has been added in lieu of the second line which excluded the amounts from buffers and cached data in “used” and added them to “free”. In Linux kernels older than 2.6.27, the reported available amount is exactly the same as the free value of the -/+ buffers/cached row from the older version of free. In later versions of the Linux kernel, free takes into account page cache and also accounts for the fact that not all reclaimable memory slabs can actually be reclaimed. For more details, see the relevant commit.