The swap file in Linux function is to store insufficient temporary data in RAM into hard disk (or SSD), then your server will access and process data in virtual memory. Yes, if in Windows this is called Virtual Memory. This will help the performance of VPS for example when it is critical memory or even has run out, so can be transferred to the swap file. If it does not exist and run out of RAM it will slow down the work of the server and often comes the error Out Of Memory.
Usually during installation any Linux distro will be prompted to create a swap partition, but now it is not necessary because the files are just as fast.
To be sure we need root access to create a swap file, and the first step is to create the file itself:
dd if=/dev/zero of=/fileswap bs=1024 count=262144
I made 256MB swap, the calculation is 1024 * 256 = 262144. You can change to a larger size, just modify the value of the count according to the result of the previous formula. The message will appear as follows:
262144+0 records in 262144+0 records out 268435456 bytes (268 MB) copied, 22.185 s, 12.1 MB/s
Then use the swap file that has been created so that can not be accessed arbitrarily:
chmod 600 /mnt/fileswap
Continue by making your own swap:
mkswap /fileswap
Information will appear like this:
mkswap: /fileswap: warning: don't erase bootbits sectors on whole disk. Use -f to force. Setting up swapspace version 1, size = 262140 KiB no label, UUID=cef8196a-04b9-4205-8a10-12cf2b9db9ec
Finally activate the swap file earlier:
swapon /fileswap
To make this file swap active on booting then need to add it in startup:
nano /etc/fstab
Then add the following code in it:
mnt/fileswap none swap sw 0 0
Or there is a faster way: echo ‘/ mnt / fileswap none swap sw 0 0’ >> / etc / fstab. Same thing. Completed already process. 😉 How do we know if the swap file exists or has been successfully added? Type the Linux memory check command:
free -m
Later will appear RAM usage information in your Linux server:
total used free shared buffers cached Mem: 243 227 16 0 69 122 -/+ buffers/cache: 35 207 Swap: 254 1 253
Notice the numbers beside the Swap text, this is the large swap file size we have created. If nothing else is 0. The way above applies to all distributions so want Ubuntu, CentOS, Debian, Arch can still be applied. Standards because. 😀 May be useful. 🙂