Introduction to Linux Swap.

About Linux Swap for Ubuntu 14.04 LTS

Usage of virtual memory is supported in Linux systems. It is used as an extension to the normal RAM, and is established on a defined part of the hard drive. When a system is experiencing a shortage of RAM, i is possible to temporarily move some of its data from RAM to Swap. Usage of a separate file in need of swapping in these circumstances will be covered in this guide. Keep in mind, however, that a whole disk partition could be created and dedicated to this purpose, it’s just that it is less flexible and requires some planning.

In general, Swap would be a great replacement for traditional RAM if only the hard drive wasn’t the slowest device in the system. If a system runs out of memory and some processes are kept in Swap, the result is that they will be slower.

How much disk space do you need to set aside for swap?

There is a rule of thumb that you should use as minimum compliance criteria:

The amount of memory allocated to Swapshould not be larger than the volume of RAM you physically have. An older recombination, the borderlines Unix/ Linux forum urbanmyth status states that states that swap space should be twice the available RAM, depending on how much HDD you have on a given system. In reality, you need as much Swap Space as your system will use, which is likely to be very little with most modern hardware setups. The only disadvantage of more Swap Space than you might need is the  finite nature of the HDD disk space allocated, which in most cases with large drives means the disadvantage is minimal.

Examples of RAM, hard drive and Swap Space:

With as much as 512MiB RAM and 30GB of hard disk space available, recommended Swap Space is 512MiB.

With as much as 512MiB RAM and 100GB HDD space, reserve 1GiB Swap since RAM volume is more limited compared to hard disk space.

With as much as 2GiB RAM and 30GB HDD, reserve 1 GiB for Swap (half the amount of RAM).

With as much as 2GiB RAM and 100GB HDD, you can use as much as 2GiB for Swap.

The actual state of Swap on your server

Use the command:

# swapon –s

to scan for any default Swap files enabled on the system.

If no Swap files exist you should see an empty list:

Filename            Type        Size    Used    Priority

Once it is confirmed there are no Swap files enabled on the virtual server, you should check how much space you have to work with on the VPS with a standard df command (-h flag puts the information in human readable format).

$ df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3        29G  4,9G   23G  18% /

none            4,0K     0  4,0K   0% /sys/fs/cgroup

udev            986M   12K  986M   1% /dev

tmpfs           200M  1,1M  199M   1% /run

none            5,0M     0  5,0M   0% /run/lock

none            996M   76K  996M   1% /run/shm

none            100M   36K  100M   1% /run/user

In this example, a Swap file of 512MB will be created on a server with 23GB of space  available from 30GB disk. Thus, it is possible to proceed.

Design the Swap file

As with the other tasks, we start by creating a new file:

Create the Swap file using the dd command:

# dd if=/dev/zero of=/testswap bs=1024 count=512k

This command is quite standard but don’t forget the following line:


This syntax sets the file’s name to ‘testswap’but you can assign any name you require. Once you have run the command, the system may be silent for a while – sometimes it needs a bit of time.


Our next step is setting up a Linux swap area:

# mkswap /testswap

Setting up Swap Space version 1, size = 524284KiB

no label, UUID=a527ab2b-12d4-49a8-92ee-fe51fa9646c0

To complete the operation, activate the Swap file:

# swapon /testswap

The Swap summary should display the new file.

# swapon -s

Filename Type Size Used Priority

/testswap file 524284 0 -1

Please note!If you stop here the file will get scraped during the next machine reboot.

You will need to edit the fstab file to mount it permanently on the virtual private server.

# nano /etc/fstab

Paste the following line at the end of the file:

/testswap none swap sw 0 0

Save and close the file (F3, Enter, F2).

Configure Swappiness

Swappiness is a programmable setting provided by the Linux kernel which controls how often the Swap file is used,. A swappiness setting = zero will mean that the Swap usage will be avoided unless absolutely necessary (where there is a risk of running out of memory altogether), while a swappiness setting of 100 will force programs to be swapped to disk almost instantly.

Ubuntu system comes with swappiness default value = 60, meaning that Swap will be used when memory usage is around 1/2 of available RAM. Check your system’s swappiness value by executing:

$ cat /proc/sys/vm/swappiness


You may decide to turn that down to 10 or 15 to balance speed and safety The Swap file will only then be used when RAM usage is around 80 or 90 percent. To change the system swappiness value, open /etc/sysctl.conf as root:

# nano /etc/sysctl.conf

You can then change or add the value on the file:

vm.swappiness = 15

This will turn your Swap into an emergency buffer that should keep your system safe from crashing if you suddenly run low on memory.

Save and exit the file, then reboot for the changes to take effect.

We recommend spending a bit of time to set things up properly, as skipping swappiness will most likely result in poor performance.

As an extra precaution you can limit permission levels on the Swap file by running:

# chown root:root /testswap

# chmod 0600 /testswap

With this done, security measures are taken care of.

This article was brought to you by VPS.net, for dedicated server hosting, cloud servers and 24/7 support visit our site here vps.net

No Comments

over 200,000 servers launched

and counting worldwide...