What is Swap Space?

For a machine to run and store the loaded applications, every processor needs data capacity.

Storage is an serious issue if you work in IT since you will have bundles of software packages to run a single application. When RAM is nearly exhausted, the Linux swap function can help you.

Using swap space instead of RAM in Linux systems can slow down the system’s performance. At the same time, there are more benefits when swap space is enabled.

In this blog, we will see the benefits of using swap space and why it is needed.

Many confuse it with virtual memory, but it is not a virtual memory but it is a part of it. We will also see the differences between virtual memory and swap space to know the difference between both of them.

Table of Contents

  1. What is swap space?
  2. Why is swap space needed?
  3. How much swap space should be used in Linux?
  4. Is swap space still necessary with so much RAM?
  5. How to increase swap space in Linux?
  6. Difference between virtual memory and swap space
  7. RAM, OOM and swap space

What is swap space?

It is essential that we understand the concept of swap space before we can understand how Linux performs when swap space is enabled.

Swap space is just a space on the hard disk which is the substitute for physical memory. It is a Linux file that stores memory. If more memory is needed by the operating system than is available, it acts as a buffer for storing data.

Swapping involves the exchange of information between the real and virtual memory of the computer and since the disk is used for swapping, it is called "swap space".


Virtual memory is a combination of the disk space and RAM that the running processes make use of. While swap space is just the portion of the virtual memory which is on the hard disk and this swap space can be used when the RAM is occupied.

Why is swap space needed?

We have learnt what is swap space. Now, let us have a look on why swap space is necessary for our system.

  1. If our RAM is very less, say 2GB or even lesser, swap space is definitely needed as most of the applications we use would have exhausted the RAM already.
  2. If hibernation is being used, then swap space must be added because the RAM’s content will be written to the swap’s partition.
  3. Swap space will be helpful if any malfunctioning program is exhausting our RAM.
  4. Sometimes using heavy applications may exhaust the RAM and using swap space here will add up stability to the system.

Does Linux needs swap space?

Using a little swap is recommended. But swap space is not the only thing Linux is needed. It needs enough amount of RAM along with swap which won’t affect the performance of your system.

Ubuntu automatically creates a swap file of 2GB in itself which tells us the importance of using swap in Linux systems.

How much swap space should be used in Linux?

It is commonly heard that swap space must be double the RAM size but it is not applicable in modern computers. There is no specific guidelines in the size of swap space.

As per Redhat’s suggestion, it is recommended to have a swap size of 20% of RAM for the modern systems which is 4GB or higher RAM.

CentOS’ suggestion is different from Redhat’s. Let us see the suggestion of CentOS.

  • Swap space should be twice the size of the RAM if RAM is lesser than 2 GB.
  • If RAM size is more than 2 GB (i.e. 5GB of swap for 3GB of RAM), then swap space must be size of RAM + 2 GB.

When it comes to Ubuntu, they take hibernation to determine the swap size.

  • If you need hibernation, then a swap of the RAM size becomes necessary for Ubuntu.
  • Otherwise, it is recommended as follows:
    • If RAM < 1 GB, swap size should be at least the size of RAM and at most double the size of RAM.
    • If RAM > 1 GB - swap size should be at least equal to the square root of the RAM size and at most double the size of RAM.
    • If hibernation is used, then swap size should be equal to size of RAM plus the square root of the RAM size.

Is swap space still necessary with so much RAM?

If we have large size of RAM like 64GB, then we barely will make use of the entire RAM space. So, it is not necessary to use swap space there.

Swap Space
Swap Space

But adding a little swap space will not do any harm and it might be helpful if in case any malfunctioning program starts using most of our RAM. Such incident is rare but if something happens like that, at that time, swap space will provide a better stability to our system.

How to increase swap space in Linux?

The first thing to do is to check whether swap space is enabled on your Linux system. To check it enter the following command in the terminal:

sudo swapon --show

You can also easily check the memory and swap space information using the free command in Linux.

free -h

Here I have 15 GB RAM and the allocated swap space memory is 2 GB.

1. Create a swap file

Use the below dd command to create a file of size 1 GB.

sudo dd if=/dev/zero of=/swap_file bs=1GB count=1

bs represents the size of the block and the count represents the number of the blocks in the above command. You can replace the value according to your requirement.

2. Secure the swap file

To secure the swap file set the permission access for the users as 600 so that they couldn't be able to read the important data from the swap file.

sudo chmod 600 /swap_file

3. Enable the swap area on swap file

Use the mkswap command to enable the swap area.

sudo mkswap /swap_file

4. Incorporate an entry in fstab file

To make the swap file persistent across every reboot add an entry in the fstab file. You can either use the echo command or vi editor to edit the fstab file.

# echo "swap_file swap  swap  defaults  0 0" >> /etc/fstab


$ vi /etc/fstab/swap_file swap swap defaults 0 0

5. Expand swap space

To enable the swap space on the file use the swapon command.

swapon /swap_file

6. Check swap space

Ensure swap space has been enabled or not with the following command:

free -m (or) swapon -s

If you want to disable the swap file you can simply use the swapoff command as below:

swapoff /swap_file

Difference between Virtual Memory and Swap Space

Is swap space a virtual memory? Where it is located? What is the difference between the virtual memory and swap space? Let us see the answers for these questions below.

Virtual Memory Swap Space
Virtual memory holds a portion of disk memory as well as RAM’s. Swap space is located at the secondary disk and is a part of virtual memory.
Virtual memory organizes the memory and solves the space issues. When RAM is full, OS moves inactive and less used processes to swap space.
Data is read/written directly from the memory by the OS. Data is never read/written directly from the memory by the OS.

RAM, OOM, and swap space

RAM (Random Access Memory) is a short-term memory that temporarily holds the data which we are working on.

Whereas, swap space is a substitute disk space for RAM when RAM’s space is full.  If the system needs more memory space, then the inactive pages in memory are moved to the swap space.

Swap space will be useful for systems with less amount of RAM but it is never a replacement for RAM. Swap space resists on hard drives which have slower access time than physical memory.

Once the RAM is used up, the swap space gets used. Since swap is slower than RAM, the performance of the system goes down. The process continues until both the RAM and Swap get exhausted.

At this point OOM (Out Of Memory) killer kicks in and kills one or more processes. OOM killer Killer is a process in Linux that the Linux kernel itself employs when the system is critically low on memory.

This situation occurs when the Linux kernel has overallocated memory to its processes.


The work swap space is to provide some space or room for the processes even when the RAM is all used up. Swap space is just the portion of virtual memory on the hard disk, and can be used when the hard disk is full.  It comes in handy for people with very less RAM in their computers.

Swap space is not only necessary for less RAM computers but also for high RAM, because it acts as another storage when RAM gets used up or exhausted.

There is a misconception that having swap space will affect our system’s performance. But in reality, if we have enough RAM in our system, having swap space won't affect our performance unless we start using swap space more than the usage of RAM.

Monitor Your Entire Application with Atatus

Atatus is a Full Stack Observability Platform that lets you review problems as if they happened in your application. Instead of guessing why errors happen or asking users for screenshots and log dumps, Atatus lets you replay the session to quickly understand what went wrong.

We offer Application Performance Monitoring, Real User Monitoring, Server Monitoring, Logs Monitoring, Synthetic Monitoring, Uptime Monitoring and API Analytics. It works perfectly with any application, regardless of framework, and has plugins.

Atatus can be beneficial to your business, which provides a comprehensive view of your application, including how it works, where performance bottlenecks exist, which users are most impacted, and which errors break your code for your frontend, backend, and infrastructure.

If you are looking for a better performance monitoring tool, you can sign up for a 14-day free trial .


#1 Solution for Logs, Traces & Metrics

tick-logo APM

tick-logo Kubernetes

tick-logo Logs

tick-logo Synthetics

tick-logo RUM

tick-logo Serverless

tick-logo Security

tick-logo More

Lydia Kirubai

Lydia Kirubai

I am a technical content writer at Atatus. Reading + Thinking + Writing makes me feel good.