Disable the swap in Ubuntu to delete


This article has been tested on the following versions of Ubuntu:

For the most part, this article applies to all versions of Ubuntu.

Linux (like almost all other operating systems) tries to keep the operating system components and currently required program data in RAM (main memory), among other things, since access to RAM is significantly faster than on data carriers such as hard drives, CDs or USB sticks.

Now it can happen that the entire RAM is full, e.g. because (very) many programs are open at the same time. At this point the Linux kernel begins to free up RAM by writing parts of the data stored in RAM to the hard drive. If the data is required again, it is reloaded into RAM and other data that is not required at the moment is written to the swap. In such a case one also says: “the system swaps”.

If a system uses the swap intensively, the system becomes significantly slower due to the frequent disk access and feels “tough”. However, if you have no swap and the RAM is full, the Linux kernel terminates programs at its own discretion in order to free up RAM. This usually results in data loss. To prevent exactly this, Ubuntu (like all other distributions) creates a swap area (= swap area) by default during installation. Newer Ubuntu versions use a swap file for this. If you are still using Xenial or if you have upgraded from an older Ubuntu version, a swap partition is used instead of the swap file. This usually has no disadvantage, but the handling differs in some details.

Swap size¶

The appropriate memory size depends on several factors and cannot generally be specified. Depending on how the system is used, around 1 GB of swap space or 1 GB + RAM capacity is sufficient for suspend-to-disk. If the swap memory (and RAM) are full, the system may crash and all data that is not synchronized with the hard disk will be lost, which is why a large swap area can be useful.

Create swap¶

As mentioned above, K | X | Ubuntu automatically creates a swap area by default, i.e. you don't have to do anything else. How to create a new swap area is described in the following section.

Create swap partition¶


The specified partition is formatted by. Recovering data stored on the partition is difficult (or impossible). So you should be sure that you are specifying the correct partition.

If you want to create a new swap partition and forgot to do so during the installation, you can create a swap partition [3] and formats them with the help of the command:

The partition can then be integrated. To do this, you first determine the UUID of the partition

and binds this with the command

a. To permanently mount the partition, open the / etc / fstab file with root rights.[2] In the file you insert the following line, which has to be adapted accordingly:

UUID = .... none swap sw 0 0

Create swap file¶

A swap file can be created with the aid of the dd program. In the following example this is 8 GB (8192 blocks with a size of 1 MB):

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

Expert info:

The line says that a file named swapfile is created in the file system with a size of 8192 MiB. The size results from 1024M × 8 = 8192 MiB, where 1M = 1024 × 1024 bytes = 1 MiB. It is important that the file is created in such a way that it then only consists of empty storage locations on the hard disk and does not contain any random data. More in the shell / dd and zero man pages.


Basically you could create a swap file with the program. However, this procedure can lead to problems with file systems such as ext4 (standard on Ubuntu), btrfs and XFS, which is why the use of is not recommended. In the man page of, the use of is also explicitly discouraged and the use of is recommended.

In order not to give any random user the opportunity to read the swap, one should

sudo chmod 0600 / swapfile

To run. This sets the rights for all users except root to "no read, no write, no execute".

To format the memory, enter the following:

You can ignore the warning. So that the system knows about the swap, you just give

a. A restart is not necessary.

You certainly want to use the swap file again after a restart. Simply open the / etc / fstab file in an editor with root rights[2] open and append this line there:

/ swapfile none swap sw 0 0

Dynamically growing swap¶

You can also install a dynamic swap file management that creates several smaller swap files in a folder as required. The Swapspace program 🇬🇧 is required for this:

Package list to copy:

sudo apt-get install swapspace

Or install with apturl, Link: apt: // swapspace

The basic configuration of Swapspace does not need to be adjusted any further.


A state of rest (suspend-to-disk) is not possible because the entire "resume" area must be present in one piece.

Set swap usage¶

You can specify when the kernel moves data from the main memory to the swap partition or swap file. The swappiness is specified with a value from 0 to 100, with 0 only swapping out if there is no other way (memory full) and with 100 the memory is hardly used. The default value is 60, so the kernel tends to swap out. No value can be recommended in general, as it depends on the respective system and user behavior. This is also shown by this discussion 🇬🇧 in which there was practically no agreement.

If the system has more than 2 GiB of RAM and the users do not handle cumbersome programs (image / video processing, data compression, elaborately designed games, etc.), it could be useful to keep the swappiness small. On the other hand, care should be taken that the capacity of the main memory is not wasted. If the swappiness has been set very low and, for example, many programs are running in parallel, it can easily happen that the main memory is almost full and exactly then a program is started that puts a particularly heavy load on the main memory. Now the memory is exhausted and the system not only has to load the already cumbersome program, but also swap it out at the same time. The result is a fully utilized system.

The current value of the swappiness can be found out with the following command:

The result could look like this:

vm.swappiness = 60

To change the swappiness immediately, e.g. to lower it to 25, use this command:

sudo sysctl vm.swappiness = 25

However, this change will be forgotten by the system after a restart if the following is not entered in the system file /etc/sysctl.conf is entered[2]:

vm.swappiness = 25

Monitor swap¶


you can check the status of the swap at any time. Normally no root rights are required for this, i.e. even unprivileged users can execute the command. If the output remains empty, no swap area has been set up.

The output shows an entry like

swapon -s Filename Type Size Used Priority / swapfile file 20971516 1272716 -2

a swap file is defined.

The command shows the contents of the file / proc / swaps at. The commands free or top offer further options for displaying swap usage. The latter continuously updates the display.

Graphic tools¶

With KDE there is a system monitor in the menu in "Programs → System → System Monitor" to find the rider "System load" sometimes also shows swap usage.

Under Xfce you can display the assignment of swap by opening the program "System → Task Manager" starts. Or by adding the "System Utilization" plugin to the Xfce panel.

Empty swap¶

The swap is not used after the system has started. If you want to empty the swap during operation, the swap must first be deactivated and then activated again. Is the swap in the / etc / fstab the commands for this are:

sudo swapoff -a ## switch off swap sudo swapon -a ## switch on swap

In between you can watch the emptying of the swap via another terminal window:

or simply

If it is only about a special swap partition, e.g. sda2, the commands are accordingly:

swapoff / dev / sda2 swapon / dev / sda2

Disable swap¶

First of all, the outsourcing should be stopped:

Then determine the responsible systemd unit:

sudo systemctl --type swap

... and mask it so that it is no longer loaded when the system starts:

sudo systemctl mask UNIT_FILE

Finally, the entry of the swap file / partition in the fstab must be deleted or at least preceded by a # commented out. The entry can be recognized by the type designation swap in the column <type>.

# UUID = 03b77228-ed4c-4218-910e-11b9f77c4b46 / ext4 defaults 01 # UUID = 8883dbc8-80f8-49b8-8c5f-13a32baefe98 none swap sw 0 0

A swap file can then simply be deleted with root rights. After removing a swap partition, you will probably want to enlarge another, see Partitioning.




This revision was created on April 8, 2021 3:13 pm by SimonovBorsig.
The following keywords were assigned to the article: data carrier, system