Thursday, 21 May 2020

What Is a Linux Swap Partition? Everything You Need to Know

linux-swap

Most Linux installations recommend that you include a swap partition. This may seem odd to Windows users used to having their entire operating system on a single partition.

What does a swap partition do, do you even need one, and how big should it be? These are all important questions that, with the right answers, can seriously improve your system’s performance.

What the Linux Swap Partition Does

Partitions in Ubuntu Linux

The swap partition serves as overflow space for your RAM. If your RAM fills up completely, any additional applications will run off the swap partition rather than RAM.

This may sound like an easy way to increase your computer’s amount of usable memory without actually getting more RAM, but that isn’t the case. RAM is the ideal hardware for memory because it’s extremely quick, unlike hard drives which are, relatively speaking, much slower.

Solid state drives may have made the performance hit less of an issue with their much-improved speeds, but even they can’t match RAM. This is also true of newer NVMe SSDs. In either case, you wouldn’t want to cause additional wear and tear on your solid state drive.

A close analogy of the swap partition is the Windows pagefile, although there are many technical differences between the two.

The Linux swap partition is not limited to being overflow storage space. It can assist your PC in other ways.

Prioritization

A swap partition can also help move some items from your memory to your hard drive in order to leave more room in memory for more important items. This implies that items that are rarely touched would get moved to the swap partition.

The threshold of what is considered “rare” depends on the “swappiness” (yes, that’s the actual term used), which is configurable. A higher swappiness means data is more likely to be moved to the swap partition. A lower swappiness means data is less likely to be moved to the swap partition.

Hibernation

A swap partition is used as the destination of your memory’s contents whenever you tell your system to hibernate. This means that without a swap partition, hibernation on Linux is impossible.

That said, it has become rather rare for people to use the hibernation feature, so this may not matter to you.

Do You Need a Swap Partition?

Create swap partition in Ubuntu Linux

Does this mean that a swap partition is necessary? Not at all! A Linux system can perform perfectly well without a swap partition. We’ve already discussed the benefits of a swap partition. Now, why might you not want to have one?

When Swap Partitions Don’t Help

Swap partitions have their downsides. They take up space on your hard drive that doesn’t resize dynamically when not in use. Heavy swap usage can also increase wear and tear on your main drive. In some cases, swap partitions don’t even help improve performance. Here’s an example where having a swap partition can actually be worse than not having one.

Say you’ve installed Linux on an old netbook with only 2GB of RAM and a 5400rpm hard drive. With just 2GB of memory, you can imagine that filling up pretty quickly with a few open browser tabs. The swap partition allows you to keep them all open as the memory overflows.

But then a bottleneck appears because of the hard drive’s 5400rpm speed. Since the hard drive is so slow and the system constantly wants to access the swap partition, the netbook becomes extremely sluggish. The machine is slow enough to be unusable unless you close everything to free up some memory.

The set swappiness doesn’t guarantee that everything in the swap partition will move back over once space becomes available in RAM. Instead, much may stay in the swap partition, causing the netbook to continue to be sluggish. So you’re left rebooting your computer to start from a clean slate, which takes a while because the system has to remove everything from the swap partition before shutting down.

What Happens When You Don’t Have Swap

If you do decide to forego a swap partition, know the risks. When your computer needs more RAM than is available, the interface can lock up. You risk having to force quit your computer and lose all the data you were working on.

In such cases, you may wish you had a swap partition around, even if it were only used that once. This depends on whether you find yourself running out of storage space often. Would you notice if you had 4GB less storage space available because you devoted that amount to swap?

Linux Swap Recommendations

Here are some recommendations for when you may want to have a swap partition and how large to make it.

  • If you would like to be able to hibernate your computer, then you should have a swap partition. The size of this partition should be the size of your installed memory, plus an additional 10-25% to leave room for any items that were already moved over into the swap partition.
  • Just want a small performance boost (and you have at least a 7200rpm hard drive)? Then you can add a swap partition if you want. The size of this can be whatever you’d like, but I wouldn’t make it any bigger than you would if you were creating a swap partition to enable hibernation.
  • If you occasionally use heavy applications that require extra RAM, a swap partition can serve as peace of mind. In this case, you don’t need your swap partition to be as large as your RAM.
  • If you have a 5400rpm hard drive, then you may not want to create a swap partition simply because the bottleneck can make your computer worse off. But if you absolutely want to have swap, then you can still create a partition using the same sizing guidelines outlined above. Just be sure to change the swappiness value to something much lower.

Changing Swappiness

Ubuntu Linux swappiness file

Like many aspects of the Linux desktop, your computer’s swappiness is stored in a text file. You can find this file by navigating to /proc/sys/vm.

When you open the file, you will see a single number indicating your current swappiness. You can edit this file using any text editor of your choice, as long as you have root permissions.

To do this with the default GNOME text editor found in Ubuntu and Fedora, you could try:

sudo gedit /proc/sys/vm/swappiness

There is also a command line option that works regardless of which text editor you have installed. Simply enter:

sudo sysctl vm.swappiness=20

You can enter any digit ranging from 0 to 100. The value indicates when you want Linux to start actively moving processes from the memory to the swap partition. So for example, a value of 20 indicates that processes will be moved when memory usage reaches 80%; the default swappiness value in Ubuntu of 60 indicates that processes will be moved when memory usage reaches 40%.

You can check whether the change was successful by reopening that text file. Unsurprisingly, the terminal offers a faster way to check your swappiness. Just enter this command:

cat /proc/sys/vm/swappiness

Does Your PC Feel Faster?

Swap partitions can make a major difference in your system’s performance—sometimes for better and sometimes for worse. Now that you know what the swap partition is for, hopefully you’re better equipped to make the decision appropriate for your situation.

But before you re-partition your drive, know that there’s more to memory management than the amount of RAM you have and the size of your Linux swap partition. Take a moment to learn how Linux manages RAM.

Read the full article: What Is a Linux Swap Partition? Everything You Need to Know



from MakeUseOf https://ift.tt/3bNj11p
via IFTTT

No comments:

Post a Comment