SWAP, or also known as Paging, is when the computer stores and retrieves data from another memory storage unit when RAM cannot store any more data. Then this data is retrieved from this secondary unit in blocks, which are called pages. This technique is quite useful for those that have programs, software, applications, or servers that need more memory than what is physically available. In this post, we will outline how to create and configure SWAP files on Linux systems. All you require is a virtual server or a droplet and full root access, things that are available with our VPS hosting in Europe.
Check whether the configuration is already enabled
There is a possibility that your system configuration already has virtual memory enabled. That’s why it’s best to check this before trying to create any other SWAP files. To find out whether it’s enabled or not, use the ‘free -h’ command. The results will appear in two lines, ‘Mem’ and ‘Swap’, and they will show the exact amount of RAM and SWAP memory used. If the server currently doesn’t use any SWAP, then this line should contain zeroes.
If you want to increase or decrease the size of allocated SWAP memory, you can follow another tutorial here: https://community.time4vps.com/discussion/293/modifying-swap-memory.
Create the Swap File
Typically, when adding SWAP files to the Linux-based systems, it’s advised to create a SWAP partition on the hard disc. Usually, this is done automatically by the system during the regular installation process. Furthermore, it’s likely the SWAP files are already set up with a partition if they are already configured. Users can still increase their virtual memory beyond the partition, but if it’s already done and managed, this might be unnecessary.
Now that you’ve checked the SWAP configuration on your Linux VPS hosting in Europe and it is not enabled, it’s time to create the SWAP File. Create an empty disk image and size it to the amount of required virtual memory. Then initialize it with the SWAP filesystem and turn it on.
The first step is to decide where you want to put it as it needs enough free space on the partition. To view the mounted filesystems & partitions, as well as their free space & sizes, you the ‘df -h’ command. Generally, it’s advised to place the SWAP file in ‘/var’ and call it ‘swap.img’.
The next step is to size the file. Although the best size depends entirely on the system and your use case, generally it’s best to use 1x or 2x the amount of available RAM. So if you have 1GB RAM, then use 1GB-2GB SWAP and so forth.
How to enable & disable SWAP
To enable the SWAP on our Linux VPS hosting in Europe, simply use the command ‘swapon’. If everything works right, there will be no output, but you can still check it out with ‘free’. To disable it, type in ‘swapoff /var/swap.img’.
It should be noted that the ‘swapon’ command only enables the SWAP files for the current boot, meaning that if you reboot the system, it will be disabled. To avoid this, you can script the ‘swapon’ to run on boot or by changing the ‘/etc/fstab’. Users should be extra careful when modifying ‘/etc/fstab’ as it can break the system if it’s overwritten or formatted incorrectly.
If you stored the swap.img in /var, you could copy & paste this command and everything should work fine:
[email protected]:/var# echo “/var/swap.img none swap sw 0 0” >> /etc/fstab
[email protected]:/var# _
Configuring priorities and sysctl settings
Once SWAP memory is enabled, you can leave it at that since it will work well with most setups or you can configure a couple of more details on our VPS hosting in Europe.
It’s a good idea to set up the order of priority of the SWAP areas if you’re planning on using SWAP across multiple devices. For this, you can either use the faster, busier storage or the more idle one, depending on your needs. To specify this priority as a parameter on ‘swapon’, add the ‘swapon -p’ line with a number after it. It should look like this:
[email protected]:/var# swapon -p 100 /var/swap.img
[email protected]:/var# swapon -p 10 /mnt/SecondDrive/swap.img
This way, the system will prioritize the SWAP area with the higher number and use it first. For ‘/etc/fstab’, add the ‘pri=’ parameter with a number after it. The priority should look like this:
/var/swap.img none swap defaults,pri=100 0 0
/mnt/SecondDrive/swap.img none swap defaults,pri=10 0 0
The ‘sysctl’ command can be used to change the setting of the Linux virtual memory manager. What you can do here is change the system’s swappiness. It controls when the system automatically chooses to move data to the SWAP file instead of RAM. The closer it is to 0, the more likely the system is to move files to SWAP memory only when it’s necessary. The closer it is to 100, the more likely it is to move data to SWAP more often.
It’s recommended to keep this as close to 0 as possible. You can check the current level with the command:
You can change the swappiness level by adding the following line at the bottom of the file:
Generally, implementing SWAP files into your Linux VPS hosting in Europe is beneficial as it increases the system’s available memory. This way, more programs, and applications can run more safely & concurrently without running into any storage issues. While these do protect you from out-of-memory errors, it’s still recommended to upgrade your plan when you start needing more memory or resources for your server.