Increase the amount of SWAP on Linux on the fly

Published by TheJoe on

Estimated reading time: 4 minutes


This article was published more than a year ago, there may have been developments.
Please take this into account.

Today Let's talk about SWAP, what it is, how much we need and how to increase it as needed. Anyone who wants can skip the preamble and move on to the explanation of how to add a SWAP file to your system.

In all modern computers a single hardware model is almost identical. Below is a diagram that helps you understand simply what we are talking about:

  • CPU register
  • Cache
  • Level 1
  • Level 2
  • RAM
    • Physical memory
    • Virtual memory (SWAP)
  • Storage devices
    • ROM / BIOS
    • Removable drives
    • Network / online storage
    • HDD
  • Input devices
    • Keyboard
    • Mouse
    • Removable drives
    • Scanner / webcam / microphone
    • Remote sources
    • Other sources

As highlighted in our list in modern operating systems, RAM management is entrusted to two distinct supports: physical memory and virtual memory.

By physical memory we mean the typical ram bank (hardware) installed in dedicated slots on the motherboard. These are electronic boards that are written and overwritten several times during work sessions. They store temporary data to allow faster access.

1GB DDR3 Memory Module
An old laptop RAM bank

Virtual memory, on the other hand, is commonly called in Linux SWAP systems (the “paging file” on Microsoft systems). This is a sector of the disk used for temporary storage (the technicians of the trade will forgive me). The speech is very complex, and more articulate than how I am explaining it, but to get a simplified overview just think that when the RAM is too little the computer tends to slow down significantly to allow the tasks to have access to resources. If RAM runs out the computer won't stop working, but some tasks may freeze or end unexpectedly. With a SWAP file or partition, on the other hand, the problem has been solved, in fact going to write on HD the excess information that should have been destined for RAM.

In the case of Linux, a SWAP partition is highly recommended, the dimensions of which must be set according to the expected use of RAM. The calculation “spannometrico” what was done in the past was to set the SWAP partition to half the RAM. With 2GB of RAM you set 1GB of SWAP.

This could be a simple and effective calculation when the computer is old, RAM is low and maybe even the disk is not large. In my case the PC is already equipped with 16GB of RAM and the disk is 1TB. I can occupy all the RAM only when I perform video rendering or large video transcoding. Instead, there are shared servers (often in business) which have to handle a lot more data than mine and the 16GB RAM may be too little. In that case, adding an 8GB SWAP partition would solve very little (if you easily occupy 16GB it won't take you long 24).

To solve this problem we thought of the SWAP file (so far we have talked about partitions). A SWAP file has the same function as the partition with the great disadvantage of having the data inside the filesystem and the great advantage of being able to be resized at will with a few quick commands. Generally, the use of the SWAP partition is recommended due to the better performance. For this reason, I recommend testing the actual use of SWAP in advance with the files and allocating a partition once you have the general picture of actual use. Another solution, not quite elegant, could be initially assign a SWAP partition which, if it is insufficient it could be integrated with a file.

Look here:  FocusWriter: a text editor, for those who are too distracted

With the sale in large quantities and the consequent decrease in the price of the new SSD disks (Solid State Drive) la RAM, albeit important, begins to play a less central role in speeding up the PC's response to the user. In fact the SSD disks with their r / w speed far superior to the old HDDs allow the execution of even very heavy software in less time, making a large amount of RAM unnecessary (in most cases).

Ok, the preamble was certainly very long, but I must say that I started from the invention of the wheel to get to the present day. Now let's see how to add a SWAP file to our Linux system with a few simple commands.

Let's understand before we begin how much memory “volatile” is installed on our system.

With the command free -h we have an overview of both physical and virtual memory installed on our system. In our case the total RAM is 15GB, while the SWAP is at 2GB.

~$ free -h
              total        used        free      shared  buff/cache   available
Mem:            15G        686M         14G        3,4M        802M         14G
Swap:          2,0G          0B        2,0G

Let's create a SWAP file

First we create an empty file of the size of 500MB with dd.

~$ sudo dd if=/dev/zero of=/swapfile1 bs=1M count=1024
1024+0 record inside
1024+0 record out
1073741824 bytes (1,1 GB, 1,0 GiB) copied, 0,945685 s, 1,1 GB/s

I created the file swapfile1 because swapfile I'm already using it. And now let's check that the allocated space is of the correct amount.

~$ ls -lh /swapfile1
-rw-r--r-- 1 root root 1,0G giu 23 15:46 /swapfile1

If we now check the amount of SWAP on the computer we will not find a trace of this latest 1GB SWAP file just created. To make it “swappabile” we give the following command:

~$ sudo mkswap /swapfile1
mkswap: /swapfile1: insecure permissions 0644, 0600 suggested.
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=d6c75786-ac0e-4501-af27-44d21b6e65e0

For now, let's ignore the error message. We activate the new SWAP space.

~$ sudo swapon /swapfile1
swapon: /swapfile1: insecure permissions 0644, 0600 suggested.

Let's change the permissions to /swapfile1 and set them up as suggested.

~$ sudo chmod 600 /swapfile1

SWAP space will indeed have been added to the system? Let's check.

~$ free -h
              total        used        free      shared  buff/cache   available
Mem:            15G        688M         13G        3,4M        1,8G         14G
Swap:          3,0G          0B        3,0G

It seems so.


I keep this blog as a hobby by 2009. I am passionate about graphic, technology, software Open Source. Among my articles will be easy to find music, and some personal thoughts, but I prefer the direct line of the blog mainly to technology. For more information contact me.


Linuccio · 15 November 2020 at 10:15 AM

Good morning, I used these instructions and they work as long as I turn off the pc. Now I have 3 gb in swap. I turn it back on and I'm there 2 gb and not three. How do I make these changes permanent? Thanks

    TheJoe · 15 November 2020 at 11:33 AM

    It shouldn't happen.
    If it actually happens at every restart you could create a script when the session starts.

Leave a Reply

Avatar placeholder

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.