Sizing Linux swap space on modern computers

For decades, a blanket recommendation for Linux swap size had been “twice RAM size”. As machine RAM climbed beyond hundreds of megabytes and hard drives climbed into tens of gigabytes of storage space, the growing default swap size wasn’t immediately noticed as a burden under nominal operating conditions. However, if a task ran amok or in the case of signal processing and remote sensing applications, too much space was allocated after an extended or large run, the system could become unusable until force-rebooted due to a swap usage and very slow hard drive (or SSD) I/O relative to RAM. This could require a site visit for remote systems that didn’t have external means of force-cycling system power, such as Intel vPro or Ethernet-connected power strip. HPC applications have unique access to high-performance drives and architectures that may justify larger swap sizes than recommended for consideration here in end-user and embedded applications.

Mitigating system lockups

When an application goes rogue, either due to an excessive request for RAM or a gradual over-consumption of RAM, the sequential write speed of the drive where swap resides is a natural lower bound on how much time the system may become unresponsive (freeze) for. An important aspect to consider is that random I/O performance, even on SSD, can be order 20x slower than sequential writes–possibly putting the SSD into the speed regime of HDD sequential I/O ~ 50 MB/s. If we suppose a 1 GB swap, this could take up to 20 seconds to fill and crash the application, allowing the system to be recovered. In reality, because the system may be trying to swap other things around at the same time, the system may freeze for minutes, even with a 1 GB swap file.


In our experience, for general end-users as well as embedded systems, it can often be better to have the application terminated automatically due to running out of RAM and swap instead of having the system become inaccessible for minutes. Thus for non-HPC or other high-resource systems, that is, on general laptops, desktop and embedded devices with capabilities of Raspberry Pi or so, we generally use about 100 MB swap size, regardless if the system has 1 GB of RAM or 32 GB of RAM. Some have advocated for no swap at all, but this can be a bit too extreme in some cases where there are number of startup tasks.

100 MB swap size is a general starting point for end-user laptop / desktop and many popular embedded systems. Consider the specific situation and test extensively before deployment to avoid unexpected impacts. {: .alert-box}

set swapfile size

Note: these notes are only for those system using a swapfile. Systems using a swap partition require advanced user Linux knowledge and are not described here to help avoid a user destroying their data.

The location and status of the Linux swap is revealed by (sudo not needed):

swapon --show

Ubuntu by default uses a swapfile, which is just a file named /swapfile. Check that this is the case for your system by:

cat /etc/fstab

In general, do not edit /etc/fstab unless you know what you are doing as mistakes can render the system unbootable.

A swapfile can be resized by turning off swapfile

swapoff -a

Create a new empty 100 MB swapfile

fallocate -l 100m /swapfile

Activate and enable the new swapfile

mkswap /swapfile

swapon /swapfile


We have never recommended using hibernation, particularly since the ubiquitous adoption of SSD as system boot time is about as fast as waking from hibernation. In that light, since before 2010 we strongly discourage using hibernation on any system in general. Sizing an enormous swap to allow hibernation only further degrades system performance and stability.