Linux swap: what it is and how to use it

If you are a Linux user you have definitely heard about Linux Swap, but you may not know what it is exactly or have only superficial knowledge about it. Time to change that!

I mention Linux swap and swappiness in almost every article on things to do after installing Linux. You know that Linux swap is somehow related to RAM and swappiness can affect your system performance. It is time to explain what it is and how to use it in little more details.

Video

What is Linux Swap?

The Linux Kernel divides RAM into chunks of memories and the swapping process is when the Linux Kernel uses a hard disk space (swap space) to store information from RAM and thus releases some RAM space. That is why when you install a Linux distribution, the installation wizard usually asks you to assign some space for the system and another for the swap.

Using swap is a very useful way to extend the RAM because it provides the necessary additional memory when the RAM space has been exhausted and a process has to be continued. It is especially recommended when you have less than 1Gb of RAM. Although in the end, everything depends on you.

Do you need Linux Swap?

This is a question many novice users ask themselves when they begin to discover Linux. In fact, this will depend on the use and amount of RAM your computer has. Regarding the use, there are processes and applications that really use a lot of memory, for example, Google Chrome. However, most of the current equipment comes with at least 8Gb of RAM and that makes the swap process less necessary. Nevertheless, having a swap space is desirable even if you have a lot of RAM.

For example, usually, when your RAM gets full and the Linux kernel has no space to write into, your system will crash. On the other hand, if you have a swap space, it will be used by the Linux kernel and your system will keep working, though much slower. So, it is safer to have swap space.

Note: swap space has one disadvantage - it is much slower than RAM. So, adding a swap space will not make your computer faster, it will only help to overcome some limitations posed by RAM size.

Linux Swap Partition

I recommend that you create the swap partition during the installation of your Linux distribution. In general, these are the recommended sizes for the swap partition.

  • If your computer has 1Gb of RAM or less, then the swap partition should be twice the size of the RAM.
  • But, If you have between 2gb and 4gb of RAM, the size of the swap partition should be half the RAM.
  • Finally, If you have more than 4gb of RAM, then it is enough to have 2Gb.

But everything depends on your use case.

You can check the type and size of your swap with this command:

swapon
Swapon output
Swapon output

Above, I have a swap partition of 2Gb.

Linux Swap File

Alternatively, you can create a Linux Swap File after the installation. The modern Linux Kernel allows Swapping to a swap file instead of a swap partition. A swap file has an advantage over a swap partition that you can change the size of your swap any time easily by changing a swap file size.

If you want to create a swap file, run this command first:

sudo fallocate -l 1G /swapfile

Note: this command is to create a 1Gb swap file. Replace 1G with the value you want.

Next, you have to set the correct permissions.

sudo chmod 600 /swapfile

Then, format the file to swap.

sudo mkswap /swapfile

Finally, enable the swap.

sudo swapon /swapfile
Creating a Linux swap file
Creating a Linux swap file

If you want the changes to be permanent, you need to edit the /etc/fstab file and add the following.

/swapfile swap swap defaults 0 0
3. Add the linux swap file to the fstab
3. Add the Linux swap file to the fstab

In the end, check the status of the swapfile:

sudo free -h
4. Check the Linux swap file status
4. Check the Linux swap file status

If you see the size of a swap file in the total column, you have done everything correctly.

How to remove a Linux Swap File

In case you need to remove a Linux swap file for any reason, you need to follow these steps.

First, deactivate the swap.

sudo swapoff -v /swapfile

If you created the entry in the /etc/fstab file, remove it. To remind you, it is the line: /swapfile swap swap defaults 0 0 entry.

Finally, delete the actual Linux Swap File.

sudo rm /swapfile
5. Removing a Linux swwap file
5. Removing a Linux swap file

How to adjust the Swappiness value

Swappiness is a property of the Linux Kernel to define how often the swap space will be used.  As you know RAM is faster than a hard drive. So, every time you need to use swap, you will notice that some processes and applications will run slower. However, you can adjust the system to use much more RAM than swap. This can help improve overall system performance.

Normally, the default swappiness value is 60. The smaller this value, the more of your RAM will be used.

To verify the swappiness value, run this command:

cat /proc/sys/vm/swappiness

You should see the value of 60.

Show the swappiness default value
Show the swappiness default value

If you want to modify the default value, you need to edit the file /etc/sysctl.conf.

sudo nano /etc/sysctl.conf

And add the following (10 is the most commonly recommended value):

vm.swappiness=10
Change the swappiness value
Change the swappiness value

Save the file and close it with Ctrl+O and Ctrl+X shortcuts. In order to apply the changes, you need to reboot the system.

This way your Linux kernel will use more RAM and less swap, but it still will swap when your RAM memory gets critically full. Usually, this setting is recommended when you have more than 4Gb of RAM.

Conclusion

In conclusion, it is safer to have some swap space on your computer. You can use either a swap partition or a swap file. The latter becomes more and more common.

Linux swap is a technical concept but knowing how it works can improve the performance of the system. Just play with the swappiness value.

There are also many other things you can do to improve your Linux experience.

Do you have anything to add about Linux swap? Have you changed the default value of swappiness? Let me know in the comments.

Please consider supporting this project:


Become a patron Buy me a coffeeBuy me a coffee Shop on Amazon Buy Linux Laptop Build Linux PC BTC: 1CfCLTYCqqDfYbngcvQtEav3n7SMZihfSS

Average Linux User
Average Linux User I am the founder of the Average Linux User project, which is a hobby I work on at night. During the day I am a scientist who uses computers to analyze genetic data.

Comments



Deb

Great and very easy to use article. I avidly follow your YouTube channel. One question: you used fallocate command. I am reading confusing views on whether fallocate works on ext4 partitions or not. Some say that dd command is more suitable for ext4 systems (e.g. https://www.howtogeek.com/455981/how-to-create-a-swap-file-on-linux/ or https://askubuntu.com/questions/1017309/fallocate-vs-dd-for-swapfile). Could you comment on this please? I am running MX Linux. Thanks a lot.



John

Hello,

This way did not work for me changing SWAPPPINESS. {Ubuntu 20.04 lts 64bit,latest updates, upgrades, & kernel}

“sudo echo “vm.swappiness=10” → /etc/sysctl.d/100-manjaro.conf vm.swappiness=10 → /etc/sysctl.d/100-manjaro.conf”. Then reboot and check your swappiness to make sure it;s 10.

Reboot and check SWAPPINESS.

Still=60


This way worked:

If you want to modify the default value, you need to edit the file /etc/sysctl.conf.

sudo nano /etc/sysctl.conf

And add the following (10 is the most commonly recommended value):

vm.swappiness=10

Save the file and close it with Ctrl+O and Ctrl+X shortcuts. In order to apply the changes, you need to reboot the system.

SWAPPINESS-10

It works very nicely, I might add!

I have a 500 TB SSD and 16 GB RAM on a Notebook. What do you suggest for the SWAPPINESS number?

thx, Brauntek


Toño VR

Hi, I’m new using Manjaro (my first distro was Ubuntu) and have some questions:

  1. Should I delete my swap partition and create a swap file?
  2. Is it better to reinstall the whole distro with no swap partition and create the swap file?
  3. My RAM is 7,5 GB and the swap partition is 4 GB. I’ve read the swap (partition/file) should be half the RAM size when it’s from 2 to 4 GB. Should I keep the 4 GB or change it to 2 GB? Thanks for your help. :)
Average Linux User

Average Linux User

Hi Toño, If you are new to Linux, I would recommend to keep things as they are. You have a good allocation of swap. The benefits of replacing a swap partition with a swap file is not worth the efforts if you do not know how to do it.

If you still what to do it, here is the sequence of steps:

  1. boot from a live image.
  2. remove the swap partition with Gparted
  3. Use Gparted to increase the system partition to the free space you got from swap.
  4. Mount your system partition to your live system, and remove swap from fstab file.
  5. Unmount the system partition and reboot.
  6. Create a swap file as shown in this article.

As you can see it, it is quite complicated. Re-installing is easier, if you have not configured your system yet. But if I were you, I would keep the swap partition. I still have a swap partition in one of my computers because I do not what to do all the steps I listed above 😉

Toño VR

Thanks! I think I’m going to create the swap file. I’m gonna try to make an installation without swap partition in another machine. As I’m newbie, I don’t wanna make a mess in my laptop, haha.

I’ve got two other questions but are not about swap:

  1. I can’t format two flashdrives I used to install Manjaro. I saw you post abut it but it didn’t work for me
  2. I’m not so sure about how to make a back up. a) I tried with Timeshift but couldn’t move the snapshot to my external HDD. Then I deleted the snapshot but the files remain and when I deleted them my system crashed so I had to reinstall Manjaro. b) I’ve reading about clonezilla, and it requires a HDD to create the backup. Everything ok with that but I’m afraid if I use my external HDD for it it’ll be formated, because I’ve got inside the external HDD my personal data and a lot of files.

May I write you in twitter to show you the screens?

jb

Hello Tono,

I first read your easy disk cleaner article, then I came here to read about the swap file. In the easy-disk cleaner article the instructions for increasing the swappiness from 60 to 10, was “sudo echo “vm.swappiness=10” → /etc/sysctl.d/100-manjaro.conf vm.swappiness=10 → /etc/sysctl.d/100-manjaro.conf”. Then reboot and check your swappiness to make sure it;s 10. The way on this page is different. Is it the same? Is it doing the exact same thing in a different way?

Thx, b0cajb

Average Linux User

Average Linux User

You can change swappiness with different methods. In Manjaro, add vm.swappiness=10 to /etc/sysctl.d/100-manjaro.conf, in Ubuntu to /etc/sysctl.conf.

Using >> is not the best way to do it. It may have some permission limitations. I recommend opening the file with nano and adding vm.swappiness=10.


herrgé

Nice article, good explanation, one comment ' vmstat' Command allows to check after swap needs in real system load conditions 'sysctl' command another tool for retrieving and forcing running kernel params, ie: $ sysctl -q vm # virtual mem. params $ sysctl -w 'sysctl setting' ***** X@host ~$ sysctl -q vm | grep 'swappiness' vm.swappiness = 60 X@host ~$ sudo sysctl -w vm.swappiness=65 [sudo] password for X : vm.swappiness = 65 X@host ~$ echo $? 0 *********** 1 question please: May I relocate a swap file or partition elsewhere than on the root system disk presumably in single user mode + fstab edition ? your welcome, best regards.


JAS

Hi ALU,

As I mentioned to you yesterday I reduced my Swappiness to 10 (adding the changes and correctly saving them in “/etc/sysctl.conf.”) However, when I checked the swappiness value using “cat /proc/sys/vm/swappiness” the old default value of 60 was shown.

After 'googling' the matter I came across this comment:

“For others stumbling onto this:

/etc/sysctl.conf is not a static file, it can be overwritten by updates or when adding/removing software. Instead you should use /etc/sysctl.d

Set the swappiness permanently like so:

Create /etc/sysctl.d/100-swappiness.conf

If you want swappiness set to i.e.5, put this into the file: vm.swappiness=5

This will not be changed by apt/yum/whatever update.”

So, after creating this file and rebooting the system and checking the swappiness value, it now showed the new value of 10.

Best Regards


JAS

Hi ALU,

Thanks very much for your video “10 Things to do first in Manjaro.” At first I hesitated on creating a swap file since my needs do not require great computing resources…wordprocessing, email, videos, music etc. (RAM usage rarely exceeding 3 GB) Anyway, for my 8 GB of RAM I created a 2 GB Swapfile and reduced the Swappiness value to 10. However, I read that swapping is not supposed to be good for SSD's.

What do you think?

Best Regards


Peter

Hello,

I followed your instructions but I am not able to run the swapon. It gives me the error Swapon failed, Invalid Argument. What can I do?

Regards Peter

P.S.: Using the latest ArchLinux



Learn how to write in Markdown with this Quick Reference.
Notify me of new comments on this post.
* E-mail is used to display Gravatar.