How to create and add Virtual Memory (Swap File) on Linux VPS Systems

Maxoq

Well-known member
Registered
Joined
Feb 25, 2015
Messages
520
Points
28
Swap is the concept of virtual memory used on the Linux operating system. When VPS/Server operates, if out of RAM the system will automatically use part of the hard drive for memory for active applications. With hosting servers running without the swap, when out of RAM the system is often automatically stop the MySQL service, and therefore error messages can appear like Establishing a Database Connection.
I want to know on how to create and add Virtual Memory (Swap File) on Linux VPS Systems?

Any guides would be great!
Max
 

VirtuBox

Well-known member
Registered
Joined
May 3, 2016
Messages
1,622
Points
83
Linux server have a better RAM management service than Windows, and can even be used with a memory amount range on VPS.
If you are running out of memory with MYSQL, check the settings, because swap was used in the past because computer was running on 512MB. But now, if you have more than 2GB RAM, you should limit swap if you have one because it will make your website really slow if it used.

I have post a tutorial for making your VPS faster : https://forumweb.hosting/13624-how-to-make-your-vps-with-ssd-faster.html
And removing swap is the part of the tutorial
 

MooseLucifer

Well-known member
Registered
Joined
May 20, 2016
Messages
149
Points
28
@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:

Code:
swapon -s
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

Code:
mkswap /swapfile
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
Code:
swapon /swapfile
Check the status of your current swap, you would see the following:

Code:
swapon -s
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)

Code:
nano /etc/sysctl.conf
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
Code:
swapoff /swapfile
Remove swap files

Code:
rm -f /swapfile
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

Code:
mkswap /swapfile
Enable swap

Code:
swapon /swapfile
Secure swap file by chmod

Code:
chown root:root /swapfile 
chmod 0600 /swapfile
Check the status of swap

Code:
swapon -s
Hope that helped!
 

ikoula

Member
Registered
Joined
Jun 10, 2016
Messages
17
Points
3
Hello,

Without swap the OS will become unstable and will eventually crash in case there no free ram left.
 
Newer Threads
Replies
5
Views
9,166
Replies
4
Views
3,908
Replies
52
Views
45,290
Replies
5
Views
4,164
Recommended Threads
Replies
28
Views
14,073
Replies
4
Views
3,818
Replies
2
Views
1,987
Replies
7
Views
1,285

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top