You are on page 1of 5

How Much Swap Should You Use in Linux?

How much should be the swap size? Should the swap be double of the RAM size or
should it be half of the RAM size? Do I need swap at all if my system has got several
GBs of RAM?

Perhaps these are the most common asked questions about choosing swap size
while installing Linux.

It’s nothing new. There has always been a lot of confusion around swap size.

For a long time, the recommended swap size was double of the RAM size but that
golden rule is not applicable to modern computers anymore. We have systems with
RAM sizes up to 128 GB, many old computers don’t even have this much of hard
disk.

But what swap size would you allot to a system with 32 GB of RAM?  64GB? That
would be a ridiculous waste of hard disk, won’t it?

Before we see how much swap size you should have, let’s first quickly know a thing
or two about swap memory. This will help you understand why swap is used.

Your system uses Random Access Memory (aka RAM) when it runs an application.


When there are only a few applications running your system manages with the
available RAM.

But if there are too many applications running or if the applications need a lot of
RAM, then your system gets into trouble. If an application needs more memory but
entire RAM is already in use, the application will crash.

Swap acts as a breather to your system when the RAM is exhausted. What happens
here is that when the RAM is exhausted, your Linux system uses part of the hard
disk memory and allocates it to the running application.
That sounds cool. This means if you allocate like 50GB of swap size, your system
can run hundreds or perhaps thousands of applications at the same time? WRONG!

You see, the speed matters here. RAM access data in the order of nanoseconds. An
SSD access data in microseconds while as a normal hard disk accesses the data in
milliseconds. This means that RAM is 1000 times faster than SSD and 100,000 times
faster than the usual HDD.

If an application relies too much on the swap, its performance will degrade as it
cannot access the data at the same speed as it would have in RAM. So instead of
taking 1 second for a task, it may take several minutes to complete the same task.
It will leave the application almost useless. This is known as thrashing in
computing terms.

In other words, a little swap is helpful. A lot of it will be of no good use.

Why is swap needed?


There are several reasons why you would need swap.

 If your system has RAM less than 1 GB, you must use swap as most applications
would exhaust the RAM soon.
 If your system uses resource heavy applications like video editors, it would be a
good idea to use some swap space as your RAM may be exhausted here.
 If you use hibernation, then you must add swap because the content of the RAM
will be written to the swap partition. This also means that the swap size should be
at least the size of RAM.
 Avoid strange events like a program going nuts and eating RAM.

Do you need swap if you have lots of RAM?


This is a good question indeed. If you have 32GB or 64 GB of RAM, chances are that
your system would perhaps never use the entire RAM and hence it would never use
the swap partition.
But will you take the chance? I am guessing if your system has 32GB of RAM, it
should also be having a hard disk of 100s of GB. Allocating a couple of GB of swap
won’t hurt. It will provide an extra layer of ‘stability’ if a faulty program starts
misusing RAM.

Can you use Linux without swap?


Yes, you can, especially if your system has plenty of RAM. But as explained in the
previous section, a little bit of swap is always advisable.

In fact, a distribution like Ubuntu automatically creates a swap file of 2 GB in size.


This should give you an indication that having some swap space is recommended.

What is the recommended swap size for Red Hat platforms?

Environment

 Red Hat Enterprise Linux 8


 Red Hat Enterprise Linux 7
 Red Hat Enterprise Linux 6
 Red Hat Enterprise Linux 5
 Red Hat OpenStack Platform (all versions)

Issue

 If I add several hundred gigabytes of RAM to a system, do I really need several hundred
gigabytes of swap space?
 What are the recommended swap size settings for Red Hat Enterprise Linux 5, Red Hat
Enterprise Linux 6, Red Hat Enterprise Linux 7, Red Hat Enterprise Linux 8?
 What are the recommended swap size settings for Red Hat OpenStack Platform?
 How much swap should I assign to a virtual guest, is the recommendation different from a
normal system?

Resolution

 In years past, Red Hat recommended a linear increase to the amount of swap space on a
system as the amount of RAM increased. Specifically, the recommendation was that
swap space should equal twice the amount of RAM when the system has up to 2 GB of
RAM installed, and the amount of RAM plus 2GB when the system has more than 2GB
of RAM installed.
 This advice is no longer practical for modern computers and modern platforms as
amounts of installed RAM have increased to multiple terabytes on some systems. The
following guidelines are a better way to determine the amount of swap space a system
should have:

Red Hat Enterprise Linux 5


Amount of installed RAM Recomended swap space

4GB or less 2GB swap space

4GB - 16GB 4GB swap space

16GB - 64GB 8GB swap space

64GB - 256GB 16GB swap space

Note: A swap space of at least 100GB is recommended for systems with over 140 logical
processors.

Red Hat Enterprise Linux 6, Red Hat Enterprise Linux 7, Red Hat Enterprise Linux
8, Red Hat OpenStack Platform (all versions)
Amount of installed RAM Recommended swap space Recommended swap space if allo

2GB or less Twice the installed RAM 3 times the amount of RAM

> 2GB - 8GB The same amount of RAM 2 times the amount of RAM

> 8GB - 64GB At least 4GB 1.5 times the amount of RAM

> 64GB or more At least 4GB Hibernation not recommended

Note: A swap space of at least 100GB is recommended for systems with over 140 logical
processors or over 3TB of RAM.

 The following items also influence the decision on how much swap space should be
allocated:
o Do specific application requirements exist? Applications may have been written with a
specific amount of swap space in mind. If this is the case, the system should be set up
with the amount of swap that is recommended by the application vendor.
o Do other requirements exist? Workstations and laptops may use hibernation functions
that store the RAM contents in the swap area. In this case, the swap space would need
to be equal to or greater than the amount of RAM installed in the system to enable
hibernation.
o Assigning swap as 'last effort' memory While the block devices hosting swap are
generally many times slower than RAM, it is useful to have swap as another layer of
memory that's available when needed. In the case of applications with high memory
utilization, swap space can allow memory to be swapped out to disk to delay or prevent
the termination of applications by the OOM killer.

 Virtual guests: for virtual guests, the same considerations as for physical systems apply.
Also for these, using a bit of swap can influence the behaviour of a process requesting
more and more memory, so the process gets slowed down first (leaving time for a
sysadmin to manually fix the situation) before eventually also the swap is exhausted and
the OOM killer terminates processes. If the memory written by the processes is not
exceeding the available swap, the system will just experience a temporary slowdown.
 Red Hat OpenStack Platform: Red Hat OpenStack Platform Director swap space is
configured at install time following the standard RHEL partitioning guidance provided
above. To configure swap space for Director deployed overcloud nodes please refer
to Solution 4005521.

You might also like