In CentOS 6 swap space or simply swap is something that is used by the operating system when the system requires more memory that the physical memory installed on the server. In that case, the operating system is using parts from the hard drives as if they were physical memory.
This sounds very good, but the only downside of this is that the hard drives are significantly slower than the RAM memory, so this will decrease the server performance in general. However, having enough memory available is very important for our server to be stable and run smooth so we should always have swap space available on our Linux server.
In some situation, is better to have the server running slow for short period, then to have the server down because it is out of memory. The server will always use the physical memory first and access the swap space only when there is not free physical memory left.
Before we add more SWAP space, we need to create the swap file. We will use the
dd command to create the swap file.
dd is built in command in CentOS whose primary purpose is to convert and copy a file .
In this case
if (input file) is
of (output file) is the destination where we want to save the file. Basically, we are going to create 1GB file that will consist of zeros only and use that as a swap later.
# dd if=/dev/zero of=/swapfile bs=1M count=1024
Next, we need to set up file so it is can be used as swap space. We will use
mkswap command for that:
# mkswap /swapfile
The swap file must be owned by root and have proper permissions:
#chown root:root /swapfile #chmod 0600 /swapfile
Once this is done, the only thing left is to activate and start using the swap space.
# swapon /swapfile
That’s it. We got another 1GB of swap space. If we wont to keep the space active after system reboot, we need to add an entry into
/etc/fstab file. Open
/etc/fstab file using your favorite text editor and add the following entry:
/swapfile swap swap defaults 0 0
Save and close the file. Next time Linux comes up after reboot, it enables the new swap file for you automatically.
We can verify that the swap was added by executing the command, the output should look like the following:
# free -m total used free shared buffers cached Mem: 2026 990 1036 0 129 620 -/+ buffers/cache: 240 1786 Swap: 1024 0 1024
In order to determine how often swap file is used (compared to the RAM usage) system uses system property that is called swappiness. Swappiness is a property of the Linux kernel that changes the balance between swapping out runtime memory, as opposed to dropping pages from the system page cache. Swappiness can be set to values between 0 and 100 inclusive.
A low value means the kernel will try to avoid swapping as much as possible where a higher value instead will make the kernel aggressively try to use swap space. The default value is 60. We can set the swappiness with the following command:
Now you have swap file on your system, which means you have more memory for the system to work with. This will make your system more stable at the cost of HDD space used by the swap file. Seems like a good deal.