One of the easiest way of increasing the responsiveness of your server and guarding against out of memory errors in your applications is to add some swap space. Swap is an area on a hard drive that has been designated as a place where the operating system can temporarily store data that it can no longer hold in RAM.
Basically, this gives you the ability to increase the amount of information that your server can keep in its working “memory”, with some caveats. The space on the hard drive will be used mainly when space in RAM is no longer sufficient for data.
The information written to disk will be slower than information kept in RAM, but the operating system will prefer to keep running application data in memory and use swap for the older data. Overall, having swap space as a fall back for when your system’s RAM is depleted is a good safety net.
Check your System for Swap Information
Before we add a swap file to our system, we want to make sure we do not already have one. Run the following command, and if you get back no results, you are in the clear:
- sudo swapon -s
Check Available Space on the Hard Drive Partition
You have to make sure you have available space on your hard drive before you begin creating your swap file.
- df -h
It should return information similar to:
udev 481M 0 481M 0% /dev
tmpfs 99M 476K 98M 1% /run
/dev/xvda1 39G 9.4G 30G 25% /
tmpfs 492M 0 492M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 492M 0 492M 0% /sys/fs/cgroup
tmpfs 99M 0 99M 0% /run/user/1000
In the above example, there are 30GB of available memory – so we can allocate 4GB for our swap file without running into any issues.
Create a Swap File
We are going to use the command, which creates a file of a pre-allocated size instantly. Run the following command, and you should see the prompt be returned to you instantly:
- sudo fallocate -l 4G /swapfile
You should then verify that the file was created correctly, by running
- ls -lh /swapfile
And you should then see information about the file you created:
Enable the Swap File
Above commands have created your swap file, but you must now tell your system to use it. This can be accomplished by only a few commands, but first you must lock the file’s permissions:
- sudo chmod 600 /swapfile
You can then run
- ls -lh /swapfile
And you will see that your file’s permissions have now been updated:
We can now proceed with setting up swap space by running the following:
- sudo mkswap /swapfile
no label, UUID=e2f1e9cf-c0a9-4ed4-b8ab-714b8a7d6944
The file is now ready to be used as swap space, which we can enable with:
- sudo swapon /swapfile
And verify by running:
- sudo swapon -s
Make the Swap File Permanent
The swap file has now been created and enabled. However, when the server restarts, it will not know to enable it on its own. This can be changed by modifying the fstab
file:
- sudo nano /etc/fstab
At the bottom of the file, add the following line and save:
You should now have a working swap file on your system. You can confirm this by running the free
command:
- free -m
Mem: 983 172 352 45 458 625
Swap: 4095 223 3872
Leave a Reply