How big should your swap partition be?

How big should your swap partition be?

Determining the size of the swap partition on a Linux system is a common question, and the short answer is: it depends. The old “rule of thumb” that recommended twice the amount of RAM no longer applies to most modern systems with large memory capacities. The decision today is based on two main factors: the amount of RAM in your system and the need for hibernation.

General Recommendations Based on RAM Memory

For most users in general-purpose scenarios, the following guidelines, based on recommendations from popular distributions such as Red Hat and Ubuntu, can be followed:

Amount of RAMRecommended Swap Size (no hibernation)
Less than 2 GBDouble the amount of RAM
2 GB to 8 GBThe same amount of RAM
8 GB to 64 GBHalf the amount of RAM, or at least 4 GB
More than 64 GBAt least 4 GB, but consider your actual needs.

For systems with a lot of RAM (above 16 GB, for example), the swap partition is unlikely to be used frequently for day-to-day tasks. In these cases, a smaller size (such as 4 GB) is usually sufficient to handle unexpected usage spikes and for kernel memory management.

The Impact of Hibernation

The most critical factor in determining swap size is hibernation (also known as “suspend to disk”). When a system hibernates, the entire contents of RAM are written to the swap partition before the computer is shut down. Upon restarting, the system reads these contents from the swap and restores the exact state it was in, with all open applications and documents.

For hibernation to work correctly, the swap partition must be at least the same size as your RAM.

For safety reasons, we recommend a slightly larger amount of space to accommodate any data that may already be in the swap.

If you don’t intend to use the hibernation feature, this requirement can be ignored, and the general recommendations can be applied.

Alternative: Swap File

A modern, more flexible alternative to a dedicated swap partition is the swap file. Instead of allocating a fixed amount of disk space during installation, a swap file is created within your main file system and can be resized as needed, without the complexity of changing disk partitions. This is the default approach in newer installations of some distributions, such as Ubuntu.

Size recommendations for a swap file follow the same logic as for a swap partition. The main advantage is the flexibility to increase or decrease swap space later.

Conclusion: What to choose?

  • General Use (no hibernation): For most users with 8 GB or more of RAM, a 4 GB to 8 GB swap is a safe and efficient choice.
  • With Hibernation: If you plan to use hibernation, the rule is clear: your swap should be at least the size of your RAM.
  • Servers and Specific Workloads: Database servers, virtualization servers, or servers running memory-intensive applications may have specific needs. In these cases, consult the software documentation for swap recommendations.
  • Low Disk Space: If disk space is a major constraint, consider a smaller swap (1-2 GB) or using a swap file, which can be created and removed as needed.

In short, analyze your hardware and your use case to make the best decision. For most modern systems, the need for large swap partitions has diminished, but having a swap area is still a best practice for system stability and performance.