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.
- 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.
- What is Swapfile.sys, how to delete Swapfile.sys?
How to create Swap files on Linux
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:
- '/mnt/1GB.swap' - this is the device name and file name
- "swap" - specifies mount mount point
- "swap sw" - displays file swap enabled by swapon - s (see below)
- '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:
- Why does Linux need no defragmentation?
- 6 ways to speed up the Ubuntu system
- 5 measures to increase Linux boot speed
You should read it
- What is Hot Swap? Benefits of Hot Swap on mechanical keyboards
- How to improve gaming performance on Linux
- How to Adjust Virtual Memory
- How to create new files in Linux
- Is it possible to run .exe files on Linux?
- How to Create ISO Files on Linux
- How to access Linux files on Windows 10
- 14 interesting Linux commands in Terminal
- File Management in Unix / Linux
- How to manage memory to restrict Linux to use too much RAM
- How to use pandoc to convert files on Linux
- What's new in Linux Kernel 5.9?