How to create file swap in Linux

Swap in Linux is specific areas on the drive, reserved for virtual memory. They are mainly used to improve computer performance when handling heavy tasks like video editing.

Swap in Linux is specific areas on the drive, reserved for virtual memory. They are mainly used to improve computer performance when handling heavy tasks like video editing. When the system has difficulty handling these tasks, the kernel will transfer inactive processes into the Swap to provide space for processes to work in working memory.

Normally, during the Linux installation process, the Swap partition is created by default and will determine the space on the hard drive for the purpose above. However, this job has some drawbacks such as space issues if you have a small drive on your old computer or use an SSD on newer devices.

  1. 7 mistakes easily 'kill' SSDs

The problem with SSDs is the ability to write its limits in cells. With the degree of wear, flash memory with finite age and multiple write times, individual drives cannot be used.

So, what is the alternative?

Using a dedicated Swap partition is impractical, so you can use Swap file as an alternative. The file swap is functional, works similarly to a partition and it also has the ability to control the size without causing size change problems on the volume.

  1. What is Swapfile.sys, how to delete Swapfile.sys?

How to create Swap files on Linux

How to create file swap in Linux Picture 1How to create file swap in Linux Picture 1

The instructions below will create a 1GB Swap file. First, create the file by entering the following command in the Terminal window:

 sudo fallocate -l 1G /mnt/1GB.swap 

If you have not installed the fallocate, you can run the following command:

 sudo dd if = / dev / zero of = / mnt / 1GB.swap bs = 1024 count = 1048576 

Now format the file Swap:

 sudo mkswap /mnt/1GB.swap 

Add Swap to the system as a file Swap:

 sudo swapon /mnt/1GB.swap 

Open '/ etc / fstab' in the text editor and add this at the end to make permanent changes:

 /mnt/1GB.swap none swap sw 0 0 

Please analyze the above command structure:

  1. '/mnt/1GB.swap' - this is the device name and file name
  2. "swap" - specifies mount mount point
  3. "swap sw" - displays file swap enabled by swapon - s (see below)
  4. '0 0' - these are the options used by the dump chapter and the corresponding fsck command

At this point, if you want to change the value 'swappiness', you can edit '/etc/sysctl.conf' in the same way when editing the above fstab. The swappiness value is usually 60, the higher the number (up to 100), the faster the exchange between virtual memory.

 vm.swappiness = 10 

The number on the swap required depends on how the operating system performs and the memory used. Users should experiment to find the best number for themselves. If the above value is set to 0, Swap file is only used when the system runs out of memory. The value above 0 will allow the system to swap idle processes and free memory to save disk space. This can improve overall system performance.

Finally, check if the Swap is working:

 sudo swapon -s 

Then, restart the computer and Swap file will act as a swap partition.

I wish you all success!

See more:

  1. Why does Linux need no defragmentation?
  2. 6 ways to speed up the Ubuntu system
  3. 5 measures to increase Linux boot speed
4.5 ★ | 2 Vote