A swap file is a form of virtual memory, similar to random access memory (RAM) and cache on a processor in which a file swap can be used to store temporary data. It's the slowest form of memory, located on an HDD or SSD, and is reserved by the Linux system for use when the OS runs out of RAM. By configuring your Linux installation to use a swap file, you can technically use more RAM than your PC has physically installed.

This would allow the OS to have more apps running simultaneously and more data loaded for immediate retrieval than would otherwise be possible. Because the swap file (or page files) is not stored using physical components, such as one of the best RAM modules on the market, we only recommend using a swap file if you're unable to install more physical system memory. By default, an operating system such as Linux will manage the swap files automatically.

Why you should use a swap file on Linux

The Ubuntu 23.10 desktop.

Using a swap file on Linux can be a cost-effective way to increase the amount of data and number of tasks that can be active at any given moment. For instance, should you wish to load up a handful of RAW image files from a camera, photo editing software will populate the available RAM with data. If you need to load more images than the RAM can handle, Linux can utilize the swap file to store the remaining images. The performance will take a hit when using the software, but it does allow you to get more done.

A swap file located on an SSD would provide a substantial performance uplift over a mechanical hard drive. Though a swap file usually sees considerable amounts of data being written to a drive, a drawback for an SSD, modern drives have been designed with some degree of endurance. An SSD released within the last few years would be suitable for a swap file and should outlast the lifetime of the PC as it was put together before an upgrade or component replacement is carried out.

How much space should be reserved for a swap file?

BashTOP running on Ubuntu 23.10.

Linux can use a permanent swap file partition to store data that cannot be loaded into RAM for whatever reason. The amount of space that should be reserved for a swap file is determined by how much system memory is present, how large the drive is where the swap file will be located, and what you plan on using the PC for. The Ubuntu website has a published FAQ that goes into deeper detail on how much space should be reserved for the swap file.

There's no wrong (or right) answer concerning the size of a swap file. See what the Linux OS sets automatically when creating partitions and go from there. The good news is a swap file can be configured later with more (or less) space reserved for temporary data. If you never find yourself running out of system memory — one of the best Linux laptops with at least 32 GB of RAM falls into this camp — you can get away without a swap file altogether.

How to check how much space is reserved on Linux for the swap file

Running the "Free" command on Ubuntu 23.10.

It's easy to check how big your swap file is on Linux. Some distributions may even have a simple GUI way of doing so, often requiring opening a system monitor — Debian-based distros will provide the System Monitor app, which can be seen in the screenshot at the top of this article. If you'd prefer to use the Terminal, here's the command you'll need:

free -h

The "free" command checks how much memory is used, installed, and available for use. The "-h" flag simply provides the results of the command in a more user-friendly way. Another command that can be used is "swapon," which lists the swap file alone if one is configured.

swapon --show

The "--show" flag ensures no changes are made to the swap file on the system; instead, it simply displays if any space is reserved on the connected storage drive(s).

Be careful when configuring the swap file on Linux, as incorrectly managing the partitions and other parts of the file system could result in data loss. Always back up your PC.

Wrapping up

I would suggest following the Ubuntu swap file guide for details on creating or editing the partition to raise or lower the available capacity for temporary data. Bear in mind that using a permanent swap file through Linux will effectively reserve space on a selected drive, limiting the overall capacity for the storage pool. This does result in lower disk activity, however.

A swap file can be a useful way to do more on your PC without resulting in expanding RAM through the purchase of new modules.