How To Add Swap on Ubuntu

About Linux Swapping

Linux RAM is composed of chunks of memory called pages. To free up pages of RAM, a “linux swap” can occur and a page of memory is copied from the RAM to preconfigured space on the hard disk. Linux swaps allow a system to harness more memory than was originally physically available.

However, swapping does have disadvantages. Because hard disks have a much slower memory than RAM, virtual private server performance may slow down considerably. Additionally, swap thrashing can begin to take place if the system gets swamped from too many files being swapped in and out.

Check for Swap Space

Before we proceed to set up a swap file, we need to check if any swap files have been enabled on the VPS by looking at the summary of swap usage.

An empty list will confirm that you have no swap files enabled:

Check the File System

After we know that we do not have a swap file enabled on the virtual server, we can check how much space we have on the server with the df command. The swap file will take 512MB— since we are only using up about 8% of the /dev/sda, we can proceed.

 

 

Create and Enable the Swap File

Now it’s time to create the swap file itself using the dd command :

 

of=/swapfile”  designates the file’s name. In this case the name is swapfile.
Subsequently we are going to prepare the swap file by creating a linux swap area:

The results display:

 

Finish up by activating the swap file:

You will then be able to see the new swap file when you view the swap summary.

 

This file will last on the virtual private server until the machine reboots. You can ensure that the swap is permanent by adding it to the fstab file.

Open up the file:

Paste in the following line:

Swappiness in the file should be set to 10. Skipping this step may cause both poor performance, whereas setting it to 10 will cause swap to act as an emergency buffer, preventing out-of-memory crashes.

You can do this with the following commands:

To prevent the file from being world-readable, you should set up the correct permissions on the swap file:

Reboot for the change to take effect.You can also change the value while your system is still running

 

you can also clear your swap by running swapoff -a and then swapon -a as root instead of rebooting to achieve the same effect.

Example:

To calculate your swap Formula

 

so what it mean is that when 10 % 395 MB of ram left then it start using swapiness.

 

Sources: https://www.digitalocean.com and http://askubuntu.com/

About Mboy 26 Articles
I'm a lover and a good friend.. :)

Be the first to comment

Leave a Reply

Your email address will not be published.


*