Swap Memory in Linux: What is “Swap Space” exactly?

Swap Memory in Linux: What is “Swap Space” exactly?

artistic robot head

Most sysadmins will have at least some familiarity with the idea of swap memory. However, even if you have come across it before, you might not know exactly what swap is, what it does, or how it works. In today’s article, we’ll be talking about swap memory in more detail to explain why it’s important and how to use it effectively.

TL;DR

“Swap memory” in linux is a file or partition on disk, where data stored in ram can be temporarily moved when there is demand for more memory than your computer has available.

What is SWAP Memory?

A SWAP file or SWAP partition is stored on one of your computer’s local drives, either a hard drive or SSD. This available disk space is called “swap memory” or “swap space”. Also known as virtual memory, this is used as a supplement to physical memory (RAM), when more RAM is needed than what’s available. To do this, less urgently needed data is copied to disk, freeing up physical memory for active programs and data. The data moved to SWAP is said to have been “swapped out”. If the data is needed again, it is “swapped in” to regular memory, by reading it from disk. This allows a computer to run smoothly even when there is a need for a little bit more memory than the computer has available.

Pros and Cons

The use of SWAP memory can be beneficial in some situations, but it can also cause problems. For example, because hard drives and SSDs are a lot slower than RAM, copying data to and from SWAP can be very slow. If this happens frequently, the entire computer can run much more slowly. Therefore, it’s helpful to have enough SWAP memory to handle an occasional spike in memory use and to move data out of ram that won’t be used for a long time. On the other hand, if actively running programs need to copy data to and from SWAP memory constantly, this can slow down the computer enough to cause it to crash outright. Therefore, beyond a certain point, adding more SWAP memory serves little purpose — if you ever need to use that much, you are in serious trouble already.

How much swap space is recommended?

I personally recommend 4GB of swap memory on a modern linux server, as a general rule of thumb.

The old advice of “take the amount of ram you have and make sure swap is half that size” is woefully outdated — this advice came about when computers typically had only a few megabytes of ram, not the tens or hundreds of gigabytes that is common today.

When large amounts of data are stored on disk as swap, the server can slow to a crawl by constantly moving large amounts of data into and out of swap. Because of this, once you’re using more than a few GB of swap space, the server can become so slow that it may lock up entirely and need to be rebooted. Although running out of both physical and virtual memory can cause individual programs to crash, that is preferable to the entire server becoming unresponsive. Because of this, I feel that 4GB provides a good balance between these factors, and is an excellent starting point — Feel free to increase or decrease this as you see fit.

IOFLOOD, when setting up our dedicated server customer’s OS for them, will use a 4GB swap partition unless a customer specifically requests something different. This can help keep a server stable when running low on memory, helping to avoid an outright crash due to processing delays from excessive swapping. This is especially useful for our customers who sell shared cPanel hosting, where spikes in apache connections can bring a large and temporary surge of memory use. By limiting swapping to a reasonable maximum, your most common worst case is that some apache threads crash, which cPanel can automatically restart, rather than the entire server becoming unreachable.

Checking your memory and swap

You can use the “free” command in Linux to see how much SWAP and physical memory your system has and how much of each is being used. You can see here how much RAM and swap are available overall and how much of each is being used right now.

Here is an example of what you might see running the “free” command:

              total        used        free      shared  buff/cache   available
Mem:       16335452     7594952     2973604      974268     5666904     7981676
Swap:       2097148      523112     1574016

The first line of the output shows the total amount of memory and swap space on the system, as well as The output’s first line displays the system’s total amount of memory and swap space, as well as how much of each is being utilized right now and how much is still available for usage. The split of physical memory (Mem) into the three categories of used, free, and buff/cache is shown on the second line. The third line displays the overall quantity of swap space as well as how much is being used right now.

You can focus on the “Swap” line to determine the amount of swap space your system has and how much of it is being used when interpreting this report. The system has a total of 2,097,148 KB (about 2GB) of swap space in the example above, of which 523,112 KB (roughly 500MB) is now in use. This indicates that the swap space is 1.5GB (or 1,574,016 KB) and is accessible for use.

The “free” command is a helpful tool for determining a system’s total amount of memory and swap space as well as how much of each is being utilized. You can gain a better understanding of the memory utilization on your system and, if required, take action to ensure that it keeps running properly by keeping an eye on this output.

When is swap used?

For a number of reasons, Linux will page data to swap space even when there is free RAM. This procedure is known as “paging.” Data that is inactive or not used regularly can be transferred to a different location to make room for data that is utilized more frequently, for example. By retaining the most crucial information in RAM and shifting less crucial information to swap space, the system is able to utilize its physical memory more effectively. One benefit here is that data read from disk can be stored in ram as part of the filesystem cache. Moving unnecessary data to SWAP leaves more ram free for this use as well.

Linux has a “swappiness” setting to specify how aggressively it will use swap space, which is another factor. The user or system administrator can change this option to regulate how much data is transferred to swap space. Even when there is spare RAM available, the system will use swap space more aggressively if the swappiness setting is high. A low value for swappiness indicates that the system will use swap space less frequently and will only move data there when absolutely necessary.

Generally speaking, Linux uses swap space to increase system performance by using the system’s memory more effectively. The system’s performance can be increased by keeping the most crucial data in physical memory by transferring less crucial or inactive data to swap space.

In conclusion, SWAP memory is an essential but frequently misunderstood subject in systems administration. Your systems can run more smoothly and minimize potential performance issues if you understand how it functions and how to control it.

Do you love servers?

We do! If you’re worried about SWAP memory and want to optimize your server’s performance, IOFLOOD can help. Our dedicated servers offer a range of solutions to fit your needs, and our servers come with plenty of RAM as standard. We also offer affordable RAM upgrades, so you can easily increase your server’s performance as your needs grow.

To learn more about our dedicated servers and how we can help you optimize your server’s performance, contact us at sales[at]ioflood.com or visit our website at https://ioflood.com. Our team is always available to answer your questions and help you find the perfect server for your needs.