One of the easiest ways to avoid application memory shortage errors is to add some Swap space to the server. When the physical memory of the system is insufficient, the Swap partition releases a part of the physical memory for the current running program. The freed space may come from programs that have not operated for a long time. The freed space is temporarily stored in Swap partitions until those programs are running, and then the saved data is restored from Swap partitions to memory.

Note: Although we recommend switching systems using traditional mechanical hard drives, for SSDs, using Swap may cause hardware problems over time. For this reason, we do not recommend that Swap be enabled on any other SSD storage. This will affect the reliability of the underlying hardware. This tutorial is more recommended for users who use traditional mechanical hard drives.

Step 1. Check the exchange of information in the system

We can first check to see if the system already has available Swap space. We can have multiple Swap files or Swap partitions, but usually one is enough.

We can see if Swap is configured by entering the following:

sudo swapon --show

If you do not receive any output, this means that your system currently does not have available WAP space.

You can use the free command card to see if there is an active swap:

free -h
total        used        free      shared  buff/cache   available
Mem:           985M         84M        222M        680K        678M        721M
Swap:            0B          0B          0B

As you can see in the Swap line of the output, there is no active swap on the system.

Step 2: Check the available space on the hard drive partition

Before we create the swap file, we will check the current disk usage to ensure that we have enough space. By input:

df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            481M     0  481M   0% /dev
tmpfs            99M  656K   98M   1% /run
/dev/vda1        25G  1.4G   23G   6% /
tmpfs           493M     0  493M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           493M     0  493M   0% /sys/fs/cgroup
/dev/vda15      105M  3.4M  102M   4% /boot/efi
tmpfs            99M     0   99M   0% /run/user/1000

The dev/vda1 in the Mounted on column is our disk. In this example, we have enough space (only 1.4 GH is used).

The appropriate size of swap space actually depends on your personal preferences and your application requirements. Usually, equal to or double the amount of system memory is a good choice. If you only use it as a RAM backup, then your swap partition should not exceed 4G as much as possible.

Step 3: Create Swap partition file

We can create a swap partition on the file system. We will allocate the swap file size we want to call in the root / directory.

The best way to create an exchange file is to use fallocate. This command creates a file of the specified size.

Since the server in our example has 1G RAM, we will create 1G files in this tutorial:

sudo fallocate -l 1G /swapfile

We can verify that it retains the correct amount of space by entering the following:

ls -lh /swapfile
-rw-r--r-- 1 root root 1.0G Apr 25 11:14 /swapfile

Step 4: Enable Swap partitioning

We need to convert swap files to swap partitions. First, we need to lock the permissions of files so that only users with root permissions can read the content.

Enter the following so that the file can only be accessed by root:

sudo chmod 600 /swapfile

Enter the following command to verify permission changes:

ls -lh /swapfile
-rw------- 1 root root 1.0G Apr 25 11:14 /swapfile

As you can see, only root users have read and write flags enabled.

We can now mark the file as swap space by entering the following

sudo mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=6e965805-2ab9-450f-aed6-577e74089dbf

After tagging the file, we can enable the swap file:

sudo swapon /swapfile

Enter the following to verify that the exchange is available:

sudo swapon --show
NAME      TYPE  SIZE USED PRIO
/swapfile file 1024M   0B   -2

We can check again with the free command:

free -h
total        used        free      shared  buff/cache   available
Mem:           985M         84M        220M        680K        680M        722M
Swap:          1.0G          0B        1.0G

Our swap has been successfully set up and our operating system will use it when necessary.

Step 5: Permanent swap file

We have enabled the swap file in the current session. However, if we restart, the server will not automatically reserve the swap settings. We can make changes by adding the exchange file to our etc/fstab'.

To avoid any problems, first back up the etc/fstabfile:

sudo cp /etc/fstab /etc/fstab.bak

Enter the following and add swap file information to the end of etc/fstabfile:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Next, we will adjust our swap space.

You can configure options that affect the performance of the system when dealing with swap.

The swappines parameter mainly configures the frequency at which the system exchanges data from RAM to switching space. The value of this parameter is a percentage between 0 and 100.

When the value is close to zero, the kernel will not exchange data to disk unless absolutely necessary. Keep in mind that interaction with swap files is "time-consuming" because they take longer than interaction with RAM, and they can lead to significant performance degradation.

When the value is close to 100, it will try to put more data into the exchange in an effort to preserve more RAM space. We can view the current swappines value by entering the following:

cat /proc/sys/vm/swappiness
60

For the server itself, you might want this value to be closer to zero. We can set swappines to different values using the sysctl command.

For example, to set swappines to 10, we can enter:

sudo sysctl vm.swappiness=10
vm.swappiness = 10

We can set this value automatically by adding the following lines to the etc/sysctl.conffile:

sudo nano /etc/sysctl.conf

At the bottom, you can add:

vm.swappiness=10

Save and close the file after completion.

Another relevant value you might want to modify is vfs_cache_pressure. This will affect how much inode and dentry information the system chooses to cache.

You can view the current value through the proc command:

cat /proc/sys/vm/vfs_cache_pressure
100

My setup system will quickly delete inode information from the cache. We can set it to a more conservative value (such as 50) by typing the following, which is 200 faster:

sudo sysctl vm.vfs_cache_pressure=50
vm.vfs_cache_pressure = 50

Again, this applies only to our current conversation. We can change it by adding it to the configuration file, just as we use the swappines settings:

sudo nano /etc/sysctl.conf

At the bottom, add the row with the specified new value:

vm.vfs_cache_pressure=50`

Save and close the file after completion.

conclusion

This tutorial will provide you with some extra space to avoid out-of-memory exceptions. Swap space can avoid some common problems. If you encounter an insufficient memory error or find that the system is unable to use the required applications, your best solution is to optimize the application configuration or upgrade the server.

Last modification：August 25th, 2019 at 07:34 pm
If you think my article is useful to you, please feel free to appreciate