Creating a SWAP partition

We will start with a quick overview of what exactly SWAP is, and when it’s needed, then we’ll look at creating a SWAP partition on our system.

SWAP is useful when we have limited RAM, under 4gb for current Gnome etc desktop environments and applications. It’s basically an extension of ram on a hard-drive (SSD or HDD (Solid-State Drive or Hard-Disk Drive)).

As we open applications they write information to the RAM, the more we open or the larger the application the more the RAM fills to a point it’s exhausted. If we had no SWAP at this point the OOM-Killer (Out-Of-Memory Killer) would kick in to try and free up space, however this usually leads to a hang or reboot as OOM-Killer can sometimes select a critical system process! By having SWAP we allow the Linux kernel to move inactive pages (sets of information in bytes) about applications we maybe opened three hours ago to the SWAP partition on our hard-drive.

“Why doesn’t RAM just get cleared out by new information?” Calling information from scratch about an application on your disk is slow (for those with spinning HDD’s), calling it from RAM is many times faster so the kernel attempts to keep this information in RAM for as long as possible, either until it’s needed again or until new information needs the space it occupies. The next best thing to RAM is to keep all the relevant information about the old application together (paged) and write it to our SWAP partition, here it can sit until we shutdown incase we re-open that application from three hours ago.

So SWAP is a way of keeping your system working with small amounts of RAM, there are lots of tweaks to tune how the kernel deals with this old information, over time i’ll try and cover most of the ones I have found to actually do something!

Lets say that we installed our Linux OS and we decided that our 4gb of RAM would be plenty, now however we’re using lots of applications simultaneously or a couple of large applications, we keep getting hangs, ‘failed to fork’ notices or reboots and we can’t just put more RAM in, we need a SWAP partition! (SWAP needs to be a continuous block of disk space for best results, hence why we are using a partition. An alternative would be to set up a SWAP file). Below is how we go about creating a SWAP partition:

First we need to know what our HDD’s are called and what the existing partition numbers are. I have used Gnome Disk Utility for a long time, it’s an easy to use nicely laid out application with useful tools and information, like SMART data (Self-monitoring, analysis and Reporting Technology), and the disk and partition names & numbers. We can also erase, format and adjust partitions, it’s a lot faster than using terminal to create a new partition, and if you’re unsure of your disk names/numbers it might be better to see it visually! As a useful quick way of checking disks and partitions we can open terminal and enter:

Sudo parted -l

This shows us the HDD names, EG: disk /dev/sda:128GB (sda= solid-state drive A. for a spinning disk it would be hda), beneath that it lists existing partitions EG:

Number   Start          End          Size         Type         File system   Flags
1              2097kB     44.6GB    44.6GB     primary    ext4               boot
2              44.6GB     127GB     82.8GB     primary    ext4

In the above we have two partitions: /dev/sda2 and /dev/sda1, we can see from the ‘flags’ that sda1 is a bootable partition, where the OS resides.

From within Gnome Disk Utility we select the disk we want to use to create our SWAP partition on. It can be the disk with the OS & /home on or a separate disk. When selected it will highlight, now click on the cogs in the lower left just beneath the partition graphic.

We want ‘Format‘. Three options will now be displayed: Erase, Type and Name.

Erase: Does what it says, it will either overwrite, or overwrite with 0’s first. Overwriting with 0’s is the best as it clears everything, however depending on the partition size it can be slow.

Type: Is what we want the partition to end up as, for this we will need ‘compatible with Linux systems (ext4)’.

Name: We can call it a name but we’ll spot it as it will always have a ‘SWAP’ flag with it.

Then we hit ‘Format‘, this will now create the new partition. Once this has completed highlight the new partition, click on the cogs again but this time we want ‘Edit partition‘.

Select ‘Linux swap (0x82)‘, then hit ‘Change

We have now created a new SWAP partition, take a note of what it’s called eg: sda2. Now we need to tell our OS that it exists and needs to use it. For this job we do need terminal:

sudo blkid /dev/sda2    — This tells us the UUID (Universally Unique Identifier) of the new SWAP partition. (We’ll use 49883f4d-88d9-482f-8a1b-90cbdf123aa8 from now on, replace this with your own UUID!)

sudo swapon -U 49883f4d-88d9-482f-8a1b-90cbdf123aa8    — This mounts and turns on the new SWAP partition immediately.

sudo gedit /etc/fstab    — This will open up fstab (file systems table) which lists all our partitions and HDD’s we want mounted automatically at boot. The first two will be the root and home partitions, beneath those lines we need to add the following line to this file to enable SWAP at every boot:

UUID=49883f4d-88d9-482f-8a1b-90cbdf123aa8     none    swap    sw      0   0    — save the file then:

sudo update-initramfs -u    — to make sure everything updates prior to rebooting.

All done! If you hibernate (I always use suspend) your PC you’ll also need to do the following:

sudo gedit /etc/initramfs-tools/conf.d/resume    — To edit initramfs file and add the line:

RESUME=UUID=49883f4d-88d9-482f-8a1b-90cbdf123aa8    — Save and exit, then:

sudo update-initramfs -u    — To make sure everything updates prior to rebooting.

We now have a SWAP partition set up and running to stop any out-of-memory issues or hangs. There are other tweaks we can do to reduce the amount of writing the OS does to SWAP, i’ll be covering these at a later stage, feel free to comment.