@the OP, as VirtuBox said, Due to the speed of hard drive is slower than RAM, especially with the server using non-SSD hard drives, so do not regularly use the Swap, if no it will reduce the performance of your system. If the status out of RAM occur, you need to optimize your server, increasing cache and upgrade RAM is the best way for this case.
With VPS uses OpenVZ virtualization technology, you probably will not create the swap because the system has automatically enabled it.
If you still want to create and add Virtual Memory (Swap File) for your VPS/server, I would have simple guides as follows
1/ Creating Swap on CentOS and Ubuntu
Check swap
Before you proceed to create swap file your should check that the current system has enabled the swap or not by running:
Without returned information or as the information below, you can create.
Code:
[root@myvps1 ~]# swapon -s
Filename Type Size Used Priority
Check free space on your disk
Use the
df -h command to check the free space. As my VPS, it has 7.3GB available, still comfortable to create swap:
Code:
[root@myvps1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 9.8G 2.0G 7.3G 22% /
tmpfs 915M 0 915M 0% /dev/shm
Create swap
Run the command dd. Here I am going to create 1GB swap (count=1024k) for the VPS 2000MB of RAM
Code:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1024k
The command above will create a swap with a capacity of 1GB. You can replace by count=1024k by count=2048k ... to create capacity 2Gb of swap. The capacity of Swap should double the physical RAM.
Code:
[root@myvps1 ~]# sudo dd if=/dev/zero of=/swapfile bs=1024 count=1024k
1048576+0 records in
1048576+0 records out
1073741824 bytes (1.1 GB) copied, 4.135 s, 260 MB/s
Create a swap partition
The output should be
Code:
[root@myvps1 ~]# mkswap /swapfile
mkswap: /swapfile: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=97698659-12dd-4368-ad7c-9c2891c178b1
Activate swap
Check the status of your current swap, you would see the following:
Code:
[root@myvps1 ~]# swapon -s
Filename Type Size Used Priority
/swapfile file 1048572 0 -1
Set your swap is activated automatically when reboot
Code:
echo /swapfile none swap defaults 0 0 >> /etc/fstab
Protect swap file by chmod
Code:
chown root:root /swapfile
chmod 0600 /swapfile
2/ Configure Swappiness
Swappiness is the level of priority to use the swap, while the remaining amount of RAM in the swappiness value (in percentage), the swap will be used. Swappiness value is between 0-100.
swappiness = 0: swap is only used when the RAM is used up.
swappiness = 10: swap used when RAM was 10%.
swappiness = 60: swap used when RAM was 60%.
swappiness = 100: precedence swap as RAM.
Due to the speed of processing data in RAM is much higher than the swap, so you should set this value closer to 0 to make the most of the power system. Best to adjust to 10.
- Check the level of use of the system swap file by running the following command
Code:
cat /proc/sys/vm/swappiness
Swappiness set parameters using the sysctl command
Code:
sysctl vm.swappiness = 10
- Check your results you will see returns 10
Code:
[root@myvps1 ~]# cat /proc/sys/vm/swappiness
10
To maintain the original parameters when restarting VPS you need to adjust parameters in the last vm.swappiness /etc/sysctl.conf file (if not, you add manually to)
At the bottom of lines, add the line
vm.swappiness = 10
Code:
# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and
# sysctl.conf(5) for more details.
#
# Use '/sbin/sysctl -a' to list all possible parameters.
# Controls IP packet forwarding
net.ipv4.ip_forward = 0
# Controls source route verification
net.ipv4.conf.default.rp_filter = 1
# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0
# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1
# Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = 1
# Controls the default maxmimum size of a mesage queue
kernel.msgmnb = 65536
# Controls the maximum size of a message, in bytes
kernel.msgmax = 65536
# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736
# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296
[B][COLOR=#FF0000]vm.swappiness = 10[/COLOR][/B]
Press Ctrl + O to save, Enter and Ctrl + X to exit.
Restart VPS and test results:
Code:
swapon -s
cat /proc/sys/vm/swappiness
The output should be
Code:
[root@myvps1 ~]# swapon -s
Filename Type Size Used Priority
/swapfile file 1048572 0 -1
[root@myvps1 ~]# cat /proc/sys/vm/swappiness
50
3/ Changing swap space
If you have followed the instructions above to create a swap file and want to change the swap space, perform the reverse process.
Turn off swap
Remove swap files
Creating new swap file to the desired size. For example, created 2GB (2048k)
Code:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=2048k
Creating a swap partition
Enable swap
Secure swap file by chmod
Code:
chown root:root /swapfile
chmod 0600 /swapfile
Check the status of swap
Hope that helped!