If you’re a Linux user, you’ve probably at least heard of swap. What is it? Well, Linux.com’s articles describes it best:

Linux divides its RAM…into chucks of memory called pages. Swapping is the process whereby a page of memory is copied to the…swap space to free up that page of memory.

Basically, when the Linux kernel decides that a chunk of memory no longer needs to be stored in RAM, it puts it onto the file or partition designated for use as swap space. Swap space is also used when the system is low on RAM to try and prevent a total crash.

Do NOT disable swap

If you’re using a lot of swap, and you think it’s slowing down your system, get more RAM, not swap. If you disable swap and your system does not have enough RAM, instead of your browser slowing down, the kernel will panic and initiate the OOM(out of memory) killer, which will begin killing processes in a last attempt to keep the system somewhat stable.

How do I create a swapfile?

Well, I’m glad you asked that 🙂 . First, you need to create a file, so you can use:

sudo fallocate -l 1G /swapfile

which is NOT recommended, read this answer on Ask Ubuntu to learn more. Or, you can do the recommended way, which is:

sudo dd if=/dev/zero of=/swapfile bs=1M count=1024

to create a 1GiB swapfile. Next, turn it into a swapfile with:

sudo mkswap /swapfile

give the proper permissions:

sudo chmod 0600 /swapfile

and enable:

sudo swapon /swapfile

Now, you can check if it’s working with free -h which will return something like(this system was already using swap for a while):

              total        used        free      shared  buff/cache   available
Mem:           488M        266M         25M         31M        196M        143M
Swap:          1.0G        267M         757M


If you’d like the swapfile to mount on boot, simply edit /etc/fstab with nano or another text editor:

sudo nano /etc/fstab

and add:

/swapfile swap sw  0 0

Save and close the file, and check free -h after a reboot.

Sources: Linux.com | Archlinux Wiki

Leave a Reply(Markdown is On)

%d bloggers like this: