From ArchWiki
(Redirected from HOW TO: Create swap file)
Jump to: navigation, search


From All about Linux swap space:



Swap space

Swap space can take the form of a disk partition or a file. Users may create a swap space during installation or at any later time as desired. Swap space can be used for two purposes, to extend the virtual memory beyond the installed physical memory (RAM), a.k.a "enable swap", and also for suspend-to-disk support.

If it is beneficial to enable swap depends on the amount of installed physical memory, and the amount of memory required to run all the desired programs. If the amount of physical memory is less than the required amount, then it is beneficial to enable swap. This avoids out of memory conditions, where the Linux kernel OOM killer mechanism will automatically attempt to free up memory by killing processes. To increase the amount of virtual memory to the required amount, add the necessary difference as swap space. For example, if your programs require 7.5 GB of memory to run, and there are 4 GB of physical memory installed, add the difference of 3.5 GB in swap space. Add more swap space to account for future requirements. It is a matter of personal preference if you prefer programs to be killed over enabling swap. The biggest drawback to enabling swap is its lower performance, see section #Performance.

To check swap status, use:

$ swapon --show


$ free -h

free also indicates if memory is running short, which can be remedied by enabling swap or increasing swap.


Swap partition



# mkswap /dev/sdxy
Warning: All data on the specified partition will be lost.

To enable the device for paging:

# swapon /dev/sdxy


UUID=<UUID> none swap defaults 0 0

where the <UUID> is taken from the command:

lsblk -no UUID /dev/sdxy

Activation by systemd


Disabling swap

To deactivate specific swap space:

# swapoff /dev/sdxy

Alternatively use the -a switch to deactivate all swap space.


Swap file




Swap file creation


# fallocate -l 512M /swapfile


# chmod 600 /swapfile

After creating the correctly sized file, format it to swap:

# mkswap /swapfile

Activate the swap file:

# swapon /swapfile


/swapfile none swap defaults 0 0

Remove swap file


# swapoff -a
# rm -f /swapfile

Finally remove the relevant entry from /etc/fstab.




Note: If the journal keeps showing the following warning systemd-swap[..]: WARN: swapFC: ENOSPC and no swap file is being created, you need to set swapfc_force_preallocated=1 in /etc/systemd/swap.conf.

Swap with USB device


  • A USB device is slower than a hard disk


Next open /etc/fstab and add




Swap encryption

See dm-crypt/Swap encryption.



Swap values can be adjusted to help performance:



To check the current swappiness value:

$ cat /sys/fs/cgroup/memory/memory.swappiness


$ cat /proc/sys/vm/swappiness

To temporarily set the swappiness value:

# sysctl vm.swappiness=10

To set the swappiness value permanently, edit a sysctl configuration file



VFS cache pressure





Or via the --priority parameter of swapon:

# swapon --priority 100 /dev/sda1


Using zswap or zram


