Create Swap File
It's often necessary to add swap to a server, especially with a virtual private server with doesn't come with any. This tutorial will show you how to add a file to your system to act as swap.
If you don't have any swap, then your processes will be killed off as soon as you run out of memory. Your Linux box will automatically move the most infrequently accessed parts of memory to swap as your memory fills up. You can even adjust how aggressive your system will try to make use of the swap file by adjusting the swappiness.
Steps
Create an empty file of the size you want your swap to be. You want this to be at a location on an SSD if possible.
Set permissions and format the file to be a swap file:
Manually mount the swap
To make sure that the swapfile is automatically mounted next time your server boots up, open your /etc/fstab
for editing.
Add the following line:
Extra Info - Removing and Re-adding Swap
Once you have added the swapfile to your fstab, you can use the following commands to unmount and mount all of your server's swap.
Conclusion
That's it. You now have a swap file that will hopefully protect your processes from being killed due to lack of available memory.
References
First published: 16th August 2018