What is a swap partition in Linux and what does it do?

what is swap partition in linux and.

Most Linux installations recommend that we include a Swap partition. This may seem strange to Windows users, who are used to creating all operating systems on one partition. So what does a Swap partition do, do we need it, and how big should it be? The following is an explanation of what is a swap partition in Linux and what it does.

What is Swap

Our system must use Random Access Memory (RAM) when running applications. When there are only a few applications running on the system, we manage with the available RAM. But if there are too many applications running or if the applications need a lot of RAM, then our system will get in trouble. If an application needs more memory but all the RAM is already used, the application will crash.

Swap acts as a reliever for our system when RAM runs out. What happens here is that when RAM runs out, our Linux system uses part of the hard disk memory and allocates it to running applications. This sounds cool. Meaning if we allocate like 50GB swap size, our system can run hundreds or even thousands of applications at the same time? but that is the WRONG thought!

Because speed matters here. RAM accesses data in the order of nanoseconds. SSDs access data in microseconds, while normal hard drives access data in milliseconds. This means that RAM is 1000 times faster than an SSD and 100,000 times faster than a regular HDD. If an application relies too much on swap, its performance will suffer because it can't access data at the same speed as in RAM. So instead of taking 1 second for a task, it may take several minutes to complete the same task. This will render the app almost useless.

Swap Advantages And Disadvantages

Advantages of Using Swap:

  • Gives space overflow when our memory is full
  • Can move rarely needed items from our high-speed memory
  • Allows us to hibernate

Disadvantages of Using Swap:

  • Takes up space on your hard drive because Swap partitions don't resize dynamically
  • Can add wear and tear to your hard drive
  • No need to increase performance

When Swap Partition Doesn't Help

Swap partitioning doesn't always help improve performance? If we have installed Linux on a laptop that only has 1GB of memory and a 5400rpm hard drive. With only 1GB of memory, we can imagine that it can fill up quickly with a few browser tabs open. SWAP partitions allow us to keep everything open because memory just overflows.

But then the bottleneck appears, due to the hard drive's 5400rpm speed. Because the hard drive is very slow, and the system constantly wants to access the Swap partition, and eventually the laptop becomes very sluggish. The swappiness set doesn't guarantee that, even if there is now space in memory, everything on the Swap partition will be moved back. Instead, many will remain on the Swap partition, causing the laptop to continue to be sluggish. This can probably only be fixed by a reboot, which takes a while as the system has to wipe everything from the SWAP partition before shutting down.

When is Swap Required?

  • If our system has less than 1 GB RAM, we should use swap because most of the applications will run out of RAM very soon.
  • If our system uses resource heavy applications like video editors, it will be a good idea to use some swap space because our RAM might run out here.
  • If we use hibernation, then we have to add swap because RAM content will be written to swap partition. This also means that the swap size must be at least the size of RAM.
  • Avoid strange occurrences such as programs going crazy which eat up RAM.

What Size Is Required To Swap?

People have different opinions about the ideal swap size. Even the major Linux distributions don't have the same swap size guidelines. If we follow Red Hat's advice, they recommend a swap size of 20% of RAM for modern systems (i.e. 4GB or higher RAM). CentOS has a different recommendation for swap partition size. They suggest twice the RAM size if RAM is less than 2 GB and RAM + 2 GB size if RAM size is more than 2 GB i.e. 5GB swap for 3GB RAM. Maybe we are confused, which one we should choose. This is why, the table below will tell us Ubuntu's recommended swap size based on RAM size and hibernation needs.

RAM Size, Swap Size (Without Hibernation), Swap Size (With Hibernation)

256MB, 256MB, 512MB

512MB, 512MB, 1GB

1GB, 1GB, 2GB

2GB, 1GB, 3GB

3GB, 2GB, 5GB

4GB, 2GB, 6GB

6GB, 2GB, 8GB

8GB, 3GB, 11GB

12GB, 3GB, 15GB

16GB, 4GB, 20GB

24GB,  5GB, 29GB

32GB,  6GB, 38GB

64GB,  8GB, 72GB

128GB,  11GB, 139GB


So What is a Swap Partition? Swap is the second type of memory in modern Linux systems. The main function of Swap space is to replace disk space for RAM memory when real RAM fills up and more space is needed. Swaps can also make a huge difference in the performance of our system, sometimes for better and sometimes for worse. For that we must be able to make the right decision when choosing it.

Hopefully, this article about What is a swap partition in Linux and what does it do?, gives you a little insight. Also, read an article about What is a Text Editor, its Functions and Examples that you may need to know. Thank you.

Previous Post Next Post