Introduction
If you want to increase performance of your Dedicated or VPS Ubuntu server and need to protect it from running out of memory you should add 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.
In other words, this gives you the ability to increase the number of concurrent applications and services that your server can run. The space on the hard drive will be used mainly when space in RAM is no longer sufficient for data.
Keep in mind that 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. This way the operating system won't stall, fail or corrupt your information for lack of RAM memory.
In this tutorial, we'll show you how to create and enable a swap file on an Ubuntu 14.04 server.
Check the System for Swap Information on Ubuntu
As a first step, is important to verify the operating system for the current swap space configuration. There can be multiple swap files or partitions.
To display swap information issue the following command:
sudo swapon -s
If you get an empty header laike the following, means the operating system has no swap file setup:
Filename Type Size Used Priority
Another, tool to check swap space on your Dedicated or VPS server is with the free utility, which shows us system memory usage. You can see the current memory and swap usage in Megabytes by typing:
free -m
You will get a result like the following:
total used free shared buffers cached
Mem: 490 244 246 0 9 167
-/+ buffers/cache: 66 423
Swap: 0 0 0
Now we have confirmed the server has no swap space by the 0
on the Swap
line.
Check Available Space on the Hard Drive
We need to make sure there is available space on the server's hard drive to be allocated for swap. We will use the simple way of adding swap space to the dedicated server by creating a new swap file.
To get disk space information issue the following command:
df -h
You will get an output like this:
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 9.4G 3.0G 6.0G 34% /
tmpfs 246M 0 246M 0% /dev/shm
If you noticed, the first line of the output shows that the VPS server has 6.0GB
You shuld know that there are many opinions as to the size of the needed swap space of the server, it really depends on your personal preferences and your application requirements. As a rule of thumb, oeprating system vendors recommend an amount equal to or double the amount of RAM on your system.
Create the Swap File
We will create a file called swapfile in our root (/) directory. The file will allocate the amount of space we need for our swap file. You will use the fallocate
program. This command creates a file of a defined size instantly.
We will create a 1GB swap file by typng:
sudo fallocate -l 1G /swapfile
You will get an output like this:
-rw-r--r-- 1 root root 1.0G May 26 12:50 /swapfile
Enable the Swap File
Now that we have created the file, we need to tell the operating system to recognize this file as a valid swap space. For that, you need to format it as swap and enable it.
For security reasons, always remember to set exclusive root
permission to write to the swap file.
sudo chmod 600 /swapfile
Verify the correct permissions by issuing this command:
-rw------- 1 root root 1.0G May 26 13:00 /swapfile
ls -lh /swapfile
Now we have cofirmation that root is the only user with write permission to this file.
The next step is to teach the operating system that this is a swap file by typing:
sudo mkswap /swapfile
You will get a confirmation like this:
Setting up swapspace version 1, size = 976562 KiB
no label, UUID=14f1e4cf-ad39-4ert-b4gb-582b8b7d6653
Now that the file has been formated as a swap file, we can enable it, as folows:
sudo swapon /swapfile
Filename Type Size Used Priority
/swapfile file 953 0 -1
The swap file has been enabled on the Ubuntu dedicated or VPS server. You can confirm the new values with the free
utility.
free -m
total used free shared buffers cached
Mem: 490 244 246 0 9 167
-/+ buffers/cache: 66 423
Swap: 953 0 953
As you can see, the operating system has 953
of new swap space available.
Make SWAP file permanent
Now that we have our swap file up and running, is important to tell the operating system to load and use it after every reboot.
In order to make the swap file permanent, modify the fstab
file by typing:
sudo nano /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sat Mar 23 10:55:48 2013
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/vda1 / ext4 defaults 1 1
/dev/vda2 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
Add the following line at the end of the file content:
/swapfile none swap sw 0 0
#
# /etc/fstab
# Created by anaconda on Sat Mar 23 10:55:48 2013
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/vda1 / ext4 defaults 1 1
/dev/vda2 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/swapfile none swap sw 0 0
Conclusion
You now have a dedicated or VPS server with added swap space. This will help you run more concurrent applications when RAM memory has been depleted by the system. Altought this is not the ideal solution to RAM shortage, it gives you a temporary option to keep your web applications running.
or