In today’s article “Add swap space on Digitalocean Droplet”, let’s learn how to make the server more secure and responsive against out-of-memory errors with help of our DigitalOcean Managed Services support at Bobcares.
How To Add Swap Space on Droplet
Swap is an area of hard drive storage that has been set aside for the operating system to temporarily store data that it can no longer hold in memory RAM. This lets you increase the amount of information that your server can keep for working memory, with some caveats.
Creating a Swap File
First, we will verify that you have available hard drive space on the system, then we can create a swap file on our filesystem. We will further allocate a file of the size that we want calling
swapfile in our root (/) directory. The suggested way of creating a swap file is with the ” fallocate ” program, this command creates a new file of the specified size.
In this guide, we will create a 1G file. You can adjust this to meet the needs of your server by below command:
sudo fallocate -l 1G /swapfileVerify that the correct amount of space has reserved:
ls -lh /swapfile-rw-r--r-- 1 root root 1.0G Apr 25 11:14 /swapfileNow our file has been created with the correct amount of space set aside.
Enabling the Swap File
As we have a file of the correct size available, we need to actually convert this into swap space.
First, we need to create permissions for the file so that only users with root privileges have access to read the contents. This prevents other users to access the file, which locks with significant security.
Make the file only accessible to the root :
sudo chmod 600 /swapfileNext, verify the permissions change:
ls -lh /swapfileOutput-rw-------1 root root 1.0G Apr 25 11:14 /swapfileBy this, only the root user has read and write flags enabled. We can next mark the file as swap space:
sudo mkswap /swapfileOutputSetting up swapspace version 1, size = 1024 MiB (1073737728 bytes) no label, UUID=6e965805-2ab9-450f-aed6-577e74089dbfAfter marking the file we need to enable the swap file, to allow our system to start using it:
sudo swapon /swapfileVerify that the swap is available:
sudo swapon --showOutputNAME TYPE SIZE USED PRIO /swapfile file 1024M 0B -2We can again check the output of the free utility to see our findings, the result will look like this:
free -hOutput total used free shared buff/cache available Mem: 981Mi 123Mi 644Mi 0.0Ki 213Mi 714Mi Swap: 1.0Gi 0B 1.0GiOur swap have completely set up and the operating system will begin to use it as necessary.
Making the Swap File Permanent
Our recent changes should have enabled the swap file for the current session. However, if we reboot the server this will not retain the swap settings automatically. Further, we can change this by adding the swap file to our
file./etc/fstabFor the safer side first back up the
file if in case anything goes wrong:/etc/fstabsudo cp /etc/fstab /etc/fstab.bakFinally, add the swap file information to the end of your
file:/etc/fstabecho '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabBy this, you will be successfully configured with a permanent swap file system.
[Looking for a solution to another query? We are just a click away.]
Conclusion
Following the steps in this Add swap space on Digitalocean Droplet guide, will show how you can avoid out-of-memory exception errors. If you find that your system is unable to use the applications, then the best solution is to optimize your application configurations or upgrade your server.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.