July 18 2023

How big should the swap partition be on Linux?

Some considerations on the usual questions about Swap, its size and virtual memory.

swap linux ram

What resonates in computer circles are the multiple opinions on the use of the Swap. Various points of view can be found: from those who follow the traditional idea, according to which Swap "must be double the RAM", to those who affirm that "today it is no longer necessary, given that the RAM we have is more than enough". At the same time, there are arguments that a "swap-free" system is faster, while others argue the opposite. With RAM becoming more affordable in 2015, the question is whether it's still worth reserving disk space for swap. The quick answer is: Yes, it's worth it. To better understand, let's analyze the situation in detail.

Virtual Memory: An Introduction

During my university studies, I had the opportunity to take two courses with prof. Montesi of the University of Bologna (he also taught in Camerino) referring to Unix systems and the text of Tanenbaum. Since then, the subject has become a deep passion of mine.

Every computing device is equipped with RAM, a fast-access memory that houses the data in use. It is very fast, much faster than any hard disk (even modern SSDs), and represents one of the main components that determine the operating speed of a machine, be it a server, a traditional computer, a smartphone, a tablet, etc.

virtual memory

Virtual memory is the sum of RAM and space that we define as Swap, which is space on a slower storage device that is used when the system needs to free up a certain amount of fast access memory (RAM).

The operating system decides what to keep ready for use and what to 'park' in a place that is less accessible, but still available. The total memory seen by the operating system is therefore the sum of RAM and Swap. Exceeding this limit will cause the system to generate OOM (Out Of Memory) messages and to manage the situation depending on the operating system in use.

Swap: Features and Management

Swap management varies between operating systems, but the basic concept remains:

Swap is space on a slower device that is used to extend RAM memory

Historically, Windows managed Swap through a file in the main file system. Its size is dynamic and grows as needs increase, it is mainly used when the machine's RAM begins to be full.

In Unix-Like systems, which include operating system families such as *BSD and GNU/Linux, Swap is usually allocated in a dedicated partition, designed and optimized for this specific purpose. This architectural choice is based on a fundamental principle: file systems are designed to manage files.

This includes implementing access permissions, tracking creation and modification times, data integrity management, and other features beyond simply reading and writing data to the device.

Adding these complexities to Swap could not only slow down the memory management process, but also add weight to the file system by making it more complex. Therefore, to avoid these problems, the Swap partition is dedicated exclusively to Swap, freeing the operating system from having to perform all these additional checks and processes.

The Swap-File option on Unix-Like systems

Despite the tradition of using dedicated partitions for swap in Unix-Like systems, there is also the possibility of using a swap file. This alternative can be particularly convenient in certain circumstances or when more flexible memory management is required.

linux swap

Swap-files, unlike Swap-partitions, are special files within the file system that can be used to temporarily store data from RAM. This option offers some flexibility as the size of a swap-file can be changed dynamically, unlike a swap-partition which has a fixed size once created.

To facilitate swap-file management, several programs have been developed. These tools allow you to dynamically manage swap-files, creating them, increasing or decreasing their size, or removing them completely, according to the real needs of the system. This flexibility can be especially useful in scenarios where system memory needs can vary widely, such as servers with variable workloads or systems with limited storage resources.

Need to Swap in the Presence of Large RAM

A question that often arises regarding the use of the Swap is the following:

If I have a lot of RAM, much more than I need, why should I create swap space anyway?

This question is logically valid and makes sense, but the answer requires a deeper understanding of how Operating Systems, such as GNU/Linux, manage memory.

There are two basic reasons why you should use Swap, even when you have a lot of RAM:

1. Optimized RAM management

First of all, Operating Systems like GNU/Linux manage RAM very intelligently, trying to make the most of it. There is no such thing as "useless" or "unused" RAM: any free space is used to cache reads and writes to disks, making I/O operations faster and more responsive.

From this point of view, Swap becomes useful because it allows you to free up RAM from applications that have not been used for a certain period of time. These are "swapped", i.e. their data in RAM is moved to Swap. This operation frees up space in RAM which can be used to increase the disk cache or to handle any load peaks. The intensity of this is governed by the swappiness setting, a kernel parameter that decides how much the system should try to use Swap relative to RAM.

2. Hibernation support

Secondly, almost all GNU/Linux distributions use Swap as data storage space in case of hibernation. During hibernation, the contents of RAM are "frozen" and copied to Swap. This allows the system to shut down completely, saving energy, but to pick up exactly where it left off the next time it starts up. To support this feature, the Swap should be at least as large as the RAM itself, to ensure that all the contents of the RAM can be saved.

In many cases, it is possible to operate even without Swap, but its presence can improve resource management efficiency, system responsiveness and the flexibility of functions such as hibernation. Therefore, even if you have a large amount of RAM, creating some swap space is often a good practice.

Swap Partition Sizing

The decision on the size to assign to the Swap partition is one of the most discussed aspects when it comes to configuring operating systems. In fact, there is no definitive answer to this question, as it varies according to the specific needs of each system and the amount of RAM available.

There was a time when it was recommended that the Swap partition should be twice the size of RAM. However, those were the years when computer memory was measured in Megabytes and not in Gigabytes as is the case today. In that context, doubling RAM was a common practice to make sure there was enough virtual memory available for system operations.

In the modern world, where RAM is measured in Gigabytes, and you often have very generous amounts of RAM, this rule is no longer so relevant. For systems that need to hibernate, a suggestion might be to give the Swap a slightly larger size than the amount of RAM available. This is because, as mentioned before, the contents of RAM are copied to Swap during hibernation.

For systems that do not need this feature, such as servers, the size of the Swap can be evaluated on a case-by-case basis. However, it is generally recommended to always reserve at least a couple of GB to handle any peak loads. If there was a sudden increase in memory requirements and it was not possible to change the partitions, one could always resort to using a swap-file, which can be fixed or dynamically allocated as needed.

An important aspect to consider is data security. Portions of RAM memory are stored in the Swap, which means that it is possible to analyze the space even after the computer is turned off and potentially find sensitive data such as passwords or personal information. For those who are especially concerned about data security, there is the option of encrypting the Swap partition. This can be done either with a fixed key, which is requested at every system startup, or with a dynamically generated key at every startup, which becomes invalid when the machine is turned off, thus increasing the level of data protection.

Do you have doubts? Don't know where to start? Contact us!

We have all the answers to your questions to help you make the right choice.

Chat with us

Chat directly with our presales support.

0256569681

Contact us by phone during office hours 9:30 - 19:30

Contact us online

Open a request directly in the contact area.

DISCLAIMER, Legal Notes and Copyright. RedHat, Inc. holds the rights to Red Hat®, RHEL®, RedHat Linux®, and CentOS®; AlmaLinux™ is a trademark of the AlmaLinux OS Foundation; Rocky Linux® is a registered trademark of the Rocky Linux Foundation; SUSE® is a registered trademark of SUSE LLC; Canonical Ltd. holds the rights to Ubuntu®; Software in the Public Interest, Inc. holds the rights to Debian®; Linus Torvalds holds the rights to Linux®; FreeBSD® is a registered trademark of The FreeBSD Foundation; NetBSD® is a registered trademark of The NetBSD Foundation; OpenBSD® is a registered trademark of Theo de Raadt; Oracle Corporation holds the rights to Oracle®, MySQL®, MyRocks®, VirtualBox®, and ZFS®; Percona® is a registered trademark of Percona LLC; MariaDB® is a registered trademark of MariaDB Corporation Ab; PostgreSQL® is a registered trademark of PostgreSQL Global Development Group; SQLite® is a registered trademark of Hipp, Wyrick & Company, Inc.; KeyDB® is a registered trademark of EQ Alpha Technology Ltd.; Typesense® is a registered trademark of Typesense Inc.; REDIS® is a registered trademark of Redis Labs Ltd; F5 Networks, Inc. owns the rights to NGINX® and NGINX Plus®; Varnish® is a registered trademark of Varnish Software AB; HAProxy® is a registered trademark of HAProxy Technologies LLC; Traefik® is a registered trademark of Traefik Labs; Envoy® is a registered trademark of CNCF; Adobe Inc. owns the rights to Magento®; PrestaShop® is a registered trademark of PrestaShop SA; OpenCart® is a registered trademark of OpenCart Limited; Automattic Inc. holds the rights to WordPress®, WooCommerce®, and JetPack®; Open Source Matters, Inc. owns the rights to Joomla®; Dries Buytaert owns the rights to Drupal®; Shopify® is a registered trademark of Shopify Inc.; BigCommerce® is a registered trademark of BigCommerce Pty. Ltd.; TYPO3® is a registered trademark of the TYPO3 Association; Ghost® is a registered trademark of the Ghost Foundation; Amazon Web Services, Inc. owns the rights to AWS® and Amazon SES®; Google LLC owns the rights to Google Cloud™, Chrome™, and Google Kubernetes Engine™; Alibaba Cloud® is a registered trademark of Alibaba Group Holding Limited; DigitalOcean® is a registered trademark of DigitalOcean, LLC; Linode® is a registered trademark of Linode, LLC; Vultr® is a registered trademark of The Constant Company, LLC; Akamai® is a registered trademark of Akamai Technologies, Inc.; Fastly® is a registered trademark of Fastly, Inc.; Let's Encrypt® is a registered trademark of the Internet Security Research Group; Microsoft Corporation owns the rights to Microsoft®, Azure®, Windows®, Office®, and Internet Explorer®; Mozilla Foundation owns the rights to Firefox®; Apache® is a registered trademark of The Apache Software Foundation; Apache Tomcat® is a registered trademark of The Apache Software Foundation; PHP® is a registered trademark of the PHP Group; Docker® is a registered trademark of Docker, Inc.; Kubernetes® is a registered trademark of The Linux Foundation; OpenShift® is a registered trademark of Red Hat, Inc.; Podman® is a registered trademark of Red Hat, Inc.; Proxmox® is a registered trademark of Proxmox Server Solutions GmbH; VMware® is a registered trademark of Broadcom Inc.; CloudFlare® is a registered trademark of Cloudflare, Inc.; NETSCOUT® is a registered trademark of NETSCOUT Systems Inc.; ElasticSearch®, LogStash®, and Kibana® are registered trademarks of Elastic NV; Grafana® is a registered trademark of Grafana Labs; Prometheus® is a registered trademark of The Linux Foundation; Zabbix® is a registered trademark of Zabbix LLC; Datadog® is a registered trademark of Datadog, Inc.; Ceph® is a registered trademark of Red Hat, Inc.; MinIO® is a registered trademark of MinIO, Inc.; Mailgun® is a registered trademark of Mailgun Technologies, Inc.; SendGrid® is a registered trademark of Twilio Inc.; Postmark® is a registered trademark of ActiveCampaign, LLC; cPanel®, LLC owns the rights to cPanel®; Plesk® is a registered trademark of Plesk International GmbH; Hetzner® is a registered trademark of Hetzner Online GmbH; OVHcloud® is a registered trademark of OVH Groupe SAS; Terraform® is a registered trademark of HashiCorp, Inc.; Ansible® is a registered trademark of Red Hat, Inc.; cURL® is a registered trademark of Daniel Stenberg; Facebook®, Inc. owns the rights to Facebook®, Messenger® and Instagram®. This site is not affiliated with, sponsored by, or otherwise associated with any of the above-mentioned entities and does not represent any of these entities in any way. All rights to the brands and product names mentioned are the property of their respective copyright holders. All other trademarks mentioned are the property of their respective registrants. MANAGED SERVER® is a European registered trademark of MANAGED SERVER SRL, with registered office in Via Flavio Gioia, 6, 62012 Civitanova Marche (MC), Italy and operational headquarters in Via Enzo Ferrari, 9, 62012 Civitanova Marche (MC), Italy.

JUST A MOMENT !

Have you ever wondered if your hosting sucks?

Find out now if your hosting provider is hurting you with a slow website worthy of 1990! Instant results.

Close the CTA
Back to top