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. 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 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

May be interested

  • 5 ways to make Linux desktops look greatPhoto of 5 ways to make Linux desktops look great
    various tools, tips and tweaks can be used to personalize linux desktops. use the following 5 methods to personalize your linux desktop environment.
  • Distinguish Ubuntu and Linux MintPhoto of Distinguish Ubuntu and Linux Mint
    linux is an open-source operating system and anyone can edit and customize to their liking, ubuntu and mint are the two operating systems born from that.
  • How to open admin and folder permissions in NautilusPhoto of How to open admin and folder permissions in Nautilus
    in nautilus on ubuntu, users can view all files and folders on their system. sometimes you may want to edit the basic file configuration like when installing for other programs. but there are some system files and folders that you cannot open and edit from nautilus unless you are open as administrator.
  • Instructions for taking screenshots of Linux computersPhoto of Instructions for taking screenshots of Linux computers
    linux is a powerful and diverse operating system, so the screen capture tools for this platform are also powerful and varied. the variety of these screeenshot tools includes simple and easy-to-use command line tools that provide scripting and process automation capabilities.
  • Instructions for installing Kali Linux via Network PXEPhoto of Instructions for installing Kali Linux via Network PXE
    starting and installing potassium over the internet (pxe) can be useful from installing a single laptop without a cd-rom or usb port until deploying a business that supports kali installation.
  • How to install Kali Linux on macOSPhoto of How to install Kali Linux on macOS
    since the release of kali linux 1.0.8, kali linux supports efi. this additional feature simplifies the process of installing and running potassium on various apple macbook air, pro and retina models.