Tuesday, September 25, 2018

How to Create a Swap File in Linux

https://www.maketecheasier.com/create-swap-file-linux

Swap within Linux are specific areas on the disk that are reserved as virtual memory. They are primarily used to enhance system performance when dealing with resource heavy tasks such as video editing. When the system starts to struggle, the kernel will move inactive processes into swap to make room for active processes within working memory.
Ordinarily, within the Linux installation, a swap partition will be created for you by default and will allocate space on the hard disk for this purpose. This has a number of drawbacks, such as space if you have a a smaller disk on an older computer, or if you are using an SSD on a newer device.
The issue with SSD drives is that they have limited write capacity within the cells. Even with wear levelling, flash memory has a finite lifespan, and multiple writes can render the individual cells unusable.
If using a dedicated swap partition is not practical, or you simply want to try an alternative and not spend money on extra RAM, then you can use a swap file instead.
A swap file functions in a similar way to a partition, although it had the added benefit of users being able to control the size without the issue of resizing a volume. In addition, how dedicated the swap will be utilized, or the “swappiness” factor, can also be controlled by modifying the swap value.
I will run through a basic example of creating a 1GB swap file.
First create the file by entering the following command within your Terminal:
If you don’t have fallocate installed, then run the more traditional command:
Now format the swap file:
Add the swap to the system as a swap file:
Open the “/etc/fstab” within your favourite text editor, and add this to the end to make the change permanent:
The line above breaks down as follows:
  • “/mnt/1GB.swap” – this is the device and file name
  • “swap” – this defines the mount point
  • “swap sw” – this shows the swap file will be activated by swapon – s (see below)
  • “0 0” – these are the options used by the dump program and the fsck command respectively
At this point, if you want to alter the “swappiness” value, then you can by editing “/etc/sysctl.conf” in the same manner as you edited the fstab above. The swappiness value is typically 60; the higher the number (up to 100) the more aggressive the swap.
The amount on swap needed depends on how the system performs and how memory is being used. Users should experiement to find what is best for them. If the value above is set to zero, then the swap file will only be used when the system has exhausted the memory. Values above zero will let the system swap out idle processes and free memory for disk caching; this can potentially improve overall system performance.
Finally, check if the swap is active:
Simply reboot and you will have a working swap file as opposed to a swap partition. Which option is best for you? Do you use a partition or use a dedicated file? Let us know in the comments and and also tell us any alternate methods you may have for generating a file.

No comments:

Post a Comment