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

  • How to Create ISO Files on LinuxHow to Create ISO Files on Linux
    today's tipsmake will show you how to turn multiple files into one iso file on a linux computer. you need to use the linux command line to do it.
  • How to Create an ISO File in LinuxHow to Create an ISO File in Linux
    this wikihow teaches you how to turn a group of files into an iso file on a linux computer. you'll use the linux command line to do this. gather your iso files in the home directory. place any files that you want to turn into an iso file...
  • Microsoft Face Swap: Very funny face transplant application for smartphonesMicrosoft Face Swap: Very funny face transplant application for smartphones
    face swap is an application developed by microsoft that allows users to pair their faces with other bodies. the application provides a large number of images for face transplantation. in addition, users can search for free images online to swap faces.
  • Manage folders in Unix / LinuxManage folders in Unix / Linux
    a directory is a file whose only task is to store the name and related file information. all files, which can be regular, special or directory files, are kept in folders.
  • How to create an EXE installation fileHow to create an EXE installation file
    sometimes to install certain software on your computer, you must use the .exe file, but have you ever wondered what the purpose of this file is and how it was created? in this article, tipsmake.com will answer that question and guide you to create an exe installation file.
  • How to completely delete a file in Linux so that it cannot be restoredHow to completely delete a file in Linux so that it cannot be restored
    in linux, files can be deleted but can still be recovered. these are things to do when you really want them to disappear.
  • How to create USB Boot Live Kali LinuxHow to create USB Boot Live Kali Linux
    the most popular and fastest way to apply kali linux is to run it from a usb drive. this method has many advantages. to do this, we first need to create a bootable usb drive from an iso image of kali linux.
  • How to use Zsync to transfer a file part in LinuxHow to use Zsync to transfer a file part in Linux
    zsync is based on rsync, another popular linux tool to synchronize files or directories, and it's very easy to use. most linux distributions already have zsync available in the package repository, so it's easy to install and get started.
  • The Cat command in LinuxThe Cat command in Linux
    the cat command (short for 'concatenate') is one of the most frequently used commands in operating systems like linux / unix. the cat command allows users to create one or more files, view file contents, join files, and redirect output in a terminal or file.
  • Learn the file system and folders on Linux operating systemsLearn the file system and folders on Linux operating systems
    linux and unix file systems are organized in a hierarchy similar to the structure of a hierarchical tree. the highest level of the file system is the root directory, denoted by a slash '/' (root directory).