What does SWAP need to be understood in a Linux environment?

It is easy to understand that the SWAP partition is an additional memory on the hard drive for system memory (RAM). If the system memory is full, SWAP will receive some memory instead of being canceled by the system. However, the SWAP partition has a problem because it is part of the drive memory (HDD or SDD), so its speed is slower than the speed of the main memory. Therefore you can't use it to replace the main memory completely.

What is the SWAP partition operating principle?

There is a common misunderstanding about the use of SWAP partitions on systems, in other words: The use of SWAP partitions affects the performance of the entire system because the speed of reading and writing on this partition is slower than the speed. Level of reading on RAM. That's totally wrong. If your system has enough RAM to operate, whether you add the SWAP partition to the system or not will not affect the system's performance, as the system does not need to switch between RAM and SWAP constantly. What if you don't have sufficient RAM? You might already know that the cost of RAM is much higher than the price of the drive. Thus you can't continually increase the system's RAM, so the SWAP partition will solve this problem. See the following example:

  • If you don't use the SWAP partition while your RAM is used, the system kernel selects one or more processes which should be removed. The choice depends on the kernel equipment algorithm.
  • If you use the SWAP partition, the system kernel will select the least used memory page to push the SWAP partition to release the RAM used for the other purpose when your RAM is used. This methodology can slow the system by a little, but it will ensure that other applications do not affect your application.

(*) Please take note that if you have a SWAP partition, you should always remember that the process is still removed by the kernel.

Is SWAP partition really necessary?

The answer depends on your situation, you should use what is the most suitable method. The reason is mentioned above: If you have enough RAM, the system does not have to dump the memory into the SWAP frequently, so it will not affect the performance of the system.

How to create a partition for SWAP?

In order to create a partition, you must have sudo permissions on the system to create a SWAP partition.

Use dd to create a swap file in the root directory of the system, where bs is the block size (block size) and count is the block number.

//The following example will guide you how to create a 4GB swap file.

sudo dd if=/dev/zero of=/swapfile bs=1G count=4

(*) bs, count: c = 1, w = 2, b = 512, kB = 1000, K = 1024,  MB = 1000 * 1000, M = 1024 * 1024, xM = M, GB = 1000 * 1000 * 1000, G = 1024 * 1024 * 1024 .
(*) With T, P, E,Z, Y will follow the same pattern

//Authorize read, write for SWAP file
sudo chmod 600 /swapfile

//Install SWAP partition
sudo mkswap /swapfile

//Allow the system to use this swap file as swap
sudo swapon /swapfile

//Check if the operations are correct
sudo swapon -s

//Allow the system to use the newly created swap partition even when rebooted
sudo vi /etc/fstab

//Add the following line
swapfile swap swap defaults 0 0

(*) Note: You can only create SWAP partitions that are smaller than the system RAM.

Credit to: Mr Duc Le Anh (ducla@uiza.io)