BLOG

KB No. 2759: MEMORY UNDER LINUX

Servers use a lot of RAM. We will explain the reasons for this phenomenon.

Background

 

First, we will take a quick look at the different types of memory in a Linux system.

 

The different memory spaces

 

 

  1. Used: memory used by processors and the kernel.
  2. Free: the memory that is available.
  3. Shared: memory that is shared by several processes at the same time. It is included in the "used" memory. Shared memory also contains the code for processes that are launched several times. The code is only loaded into memory once. It is therefore difficult to analyze the exact amount of space taken up by a process when it uses shared memory.
  4. Buffered/cache: this speeds up disk and file access. The space it occupies is available for other processes if needed. Please note that the less memory available, the less effective the disk cache mechanism will be. All DBMSs rely on this mechanism to speed up their processing. Therefore, if all memory is used, caching is no longer possible, and each disk access is more costly.
  5. SWAP: If processes are idle, they can be transferred to SWAP and free up RAM. This is not a problem as long as the machine does not run out of memory. This frees up space for processes that need it more, or for the disk cache. SWAP also takes over when almost all of the RAM is in use.

 

 How memory works

 

Linux avoids wasting memory, which is why it uses as much RAM as possible to optimize its use. Processes switch to SWAP when the value configured in swapiness is reached (in %).

To find out this value, you will need to type the command ⇒

cat /proc/sys/vm/swappiness:

User1:~/$ cat /proc/sys/vm/swappiness

60

In this example, as soon as 60% of RAM remains available, the processes will switch to SWAP.

Overuse of SWAP will cause the machine to slow down.

 In short:

 

Linux uses as much RAM as possible, even if few programs are running, in order to optimize performance. If the RAM is saturated (swapiness reached), processes switch to SWAP. If SWAP is too busy, the system will slow down.

The tools

 

There are various tools available to help you analyze and optimize your memory performance, which we will introduce in this section:

  • HTOP: launch by running the command: htop
    If the following message appears:
    root@olfeo-1:/# htop
    bash: htop: command not found
    You will need to install HTOP as follows ⇒
    apt-get update ⇒ to update the repositories
    apt-get install -y htop ⇒ launch the installation

Once the HTOP command has been launched:

At the top of the interface, you can see the CPU usage as a percentage: from 1 to x (x = number of processors).

Memory usage in MB relative to total available memory, as well as swap memory usage.

  •  The used memory appears in green.
  •  Free memory appears in black or gray.
  •  Buffers/cached memory appears in blue and yellow.

The time since the system was started is also indicated.

Next, a "table" listing the processes is displayed, where you can find a wealth of information (the user who executed it, the percentage of memory and CPU it is using, and the command used to execute it).

Finally, in the lower section, a menu explains the various actions that are available.

  • FREE:

The FREE command displays the status of the system's RAM memory and has several options.

The free command displays a result such as:root@olfeo-1:/#free

  Total Used Free Shared Buff/Cache Available
Mem: 16394968 8288476 2295200 312220 5811292 7456012
Swap: 16691196 0 16691196      

RAM and SWAP are separated into several columns representing the different states explained above. The FREE command has a few options to customize your results:

free -M⇒To display results in MB

free -k ⇒To display results in KB

free -t⇒ to display the total swap + RAM

STEPS

 

Useful commands

 

After analyzing the state of your memory and finding some inconsistencies, you will likely need to execute the following commands:

Change SWAPINESS⇒ sysctl vm.swappiness=10

Enable and disable the SWAP partition for the settings to take effect. `
` This change will only be effective until the next system reboot.

To permanently change SWAPINESS⇒
Add the following line to the /etc/sysctl.conf file
vim /etc/sysctl.conf`
vm.swappiness=10`

Clear the memory cache ⇒
`sysctl -w vm.drop_caches=3`

Enable and disable the SWAP partition

  • Find out the SWAP partitions mounted in GB: swapon –show SIZE

root@olfeo-1:/#swapon –show SIZE

NAME TYPE SIZE USED PRIO

/dev/dm-1 partition 15.9G 0B -1

  • Unmount the SWAP partitions: swapoff -av (This operation empties the contents of the swap in RAM, so free > SWAP used is required).

root@olfeo-1:/#swapoff -av

swap off /dev/dm-1

  • Reactivate SWAP partitions: `swapon -av`

root@olfeo-1:/#swapon -av

swapon: /dev/mapper/aparolari–vg-swap_1: found signature [pagesize=4096, signature=swap]
swapon: /dev/mapper/aparolari–vg-swap_1: page size: 4096, swap space size: 17091788800, device size: 17091788800
swapon /dev/mapper/aparolari—vg-swap₁

In conclusion, the fact that your RAM is almost fully utilized is not necessarily a sign of malfunction or slowdown.

However, the SWAP should never be filled, as this will slow down the system.