others-how to extend linux swap space when my swap space is low ?

Problem

When we check our vps server, we get this ‘swap space or partition is low’ warning in the vps control panel :

swap1

The core error is :

Your server's SWAP space is too small, now the SWAP space is low, you should solve this problem

Environment

  • Linux

Reason

You have allocated a small SWAP space for the server, now it’s too low to serve your workloads on the server. You should extend your SWAP space NOW!!!

Solution

STEP 1: create a swap file to be used as SWAP partition

You can do as follows:

dd if=/dev/zero of=/mnt/swap bs=1M count=1024

The above command would create a swap file named /mnt/swap with 1G size, and the parameter “bs” specifies the block size for the both the input and output file.

What is dd command in liunx?

dd is a command-line utility for Unix and Unix-like operating systems whose primary purpose is to convert and copy files.

  • On Unix, device drivers for hardware (such as hard disk drives) and special device files (such as /dev/zero and /dev/random) appear in the file system just like normal files.
  • dd can also read and/or write from/to these files, provided that function is implemented in their respective drivers
  • As a result, dd can be used for tasks such as backing up the boot sector of a hard drive, and obtaining a fixed amount of random data.
  • The dd program can also perform conversions on the data as it is copied, including byte order swapping and conversion to and from the ASCII and EBCDIC text encodings.

And we use /dev/zero because:

  • /dev/zero produces a continuous stream of NULL (zero value) bytes.

You can get more information about dd command on this site.

STEP 2: Make the file as SWAP space

We can set the above file as linux SWAP space as follows:

mkswap /mnt/swap

What is mkswap?

mkswap sets up a Linux swap area on a device or in a file.

STEP 3: Turn on the swap space

We can turn on the swap space as follows:

swapon /mnt/swap

What is swapon?

swapon is used to specify devices on which paging and swapping are to take place.

Now our swap space is on duty. You can check your swap space by running:

free -m

There should be a swap line in the output of this command.

[[email protected]]# sudo free -m
             total       used       free     shared    buffers     cached
Mem:          1008        669        338          0         20        304
-/+ buffers/cache:        343        664
Swap:         1023          0       1023

You can see that the swap space is 1G , just the same as what we specified in the above commands.

STEP 4[optional]: Persist the swap settings

If you want to have the same settings even if the server restarts, you can add this line to the bottom of the /etc/fstab file:

/mnt/swap swap swap defaults 0 0

What is /etc/fstab:

Your Linux system’s filesystem table, aka fstab, is a configuration table designed to ease the burden of mounting and unmounting file systems to a machine. It is a set of rules used to control how different filesystems are treated each time they are introduced to a system. Consider USB drives, for example. Today, we are so used to the plug and play nature of our favorite external drives that we may completely forget that operations are going on behind the scenes to mount the drive and read/write data.

There are 6 columns of each line:

Table structure

The table itself is a 6 column structure, where each column designates a specific parameter and must be set up in the correct order. The columns of the table are as follows from left to right:

  • Device: usually the given name or UUID of the mounted device (sda1/sda2/etc).
  • Mount Point: designates the directory where the device is/will be mounted.
  • File System Type: nothing trick here, shows the type of filesystem in use.
  • Options: lists any active mount options. If using multiple options they must be separated by commas.
  • Backup Operation: (the first digit) this is a binary system where 1 = dump utility backup of a partition. 0 = no backup. This is an outdated backup method and should NOT be used.
  • File System Check Order: (second digit) Here we can see three possible outcomes. 0 means that fsck will not check the filesystem. Numbers higher than this represent the check order. The root filesystem should be set to 1 and other partitions set to 2.

STEP 5[Optional]: Set the threshold of swap

We can set the threshold of the swapping to control when the system should use the swap space as follows:

Open your /etc/sysctl.conf file and add this line:

vm.swappiness = 10

Verify the settings:

sysctl -p

What is vm.swappiness?

vm.swappiness

The default value of vm.swappiness is 60 and represents the percentage of the free memory before activating swap. The lower the value, the less swapping is used and the more memory pages are kept in physical memory.

The value of 60 is a compromise that works well for modern desktop systems. A smaller value is a recommended option for a server system, instead. As the Red Hat Performance Tuning manual points out [8], a smaller swappiness value is recommended for database workloads. For example, for Oracle databases, Red Hat recommends a swappiness value of 10. In contrast, for MariaDB databases, it is recommended to set swappiness to a value of 1 [9].

Changing the value directly influences the performance of the Linux system. These values are defined:

* 0: swap is disable * 1: minimum amount of swapping without disabling it entirely * 10: recommended value to improve performance when sufficient memory exists in a system * 100: aggressive swapping

Verify

Now we can goto the control panel of our server again, we get this:

屏幕快照 2021-02-27 16.47.36

It works!

By the way

If you encounter this error:

mkswap: error: swap area needs to be at least 40 KiB

The reason is that the specified SWAP partition file is too small, and the SWAP partition file should be at least larger than 40KB.

Just use bigger swap size to create your swap file.

-->