Skip to main content

Managing swap in the modern Linux system

Managing swap space is somewhat of a lost art. Learn the way of the swap master.
Image
computer RAM

Photo by Valentine Tanasovich from Pexels

Swap space is an integral part of the modern Linux system, especially in the Enterprise environments of today. Many of you may be familiar with its concepts and how to manage it to work in your favor. I would bet that there are a lot of people who, like myself until recently, haven't interfaced with swap or the manipulation of it in a live environment and may not entirely understand its purpose. I want to go through the basic concepts and explain how to create and manage a swap space. 

What is swap?

Have you ever seen the movie Inception? The premise is centered around "a dream within a dream." Well, swap is similar in concept because it is virtual memory built on physical memory. Confusion may set in without a common definition of "memory." In computing, there are two main types of memory -  RAM and hard disk. 

Random-access memory, also known as RAM, is the memory used to store and access programs being actively used by the computer. This memory is very different from the hard drive storage that you have on your computer. Whether it's disk-based or solid-state, your Hard drive memory is used to store data not actively being used on the computer. It also allows for the data to be retained through power cycles. With RAM, it is considered volatile memory, meaning that the RAM is cleared when the computer is cycled off.

Why is swap?

Now that I've defined those types of memory, you need to understand why swap is necessary. RAM is a finite resource, and when you're multitasking or accessing demanding files or software, you can exceed the amount of RAM onboard the system. When this happens, it can cause programs to crash or freeze until the needed RAM becomes available. This is where swap comes in.

Swap allows you to create virtual RAM using your existing physical hard drive space when your physical RAM is being pushed to capacity. Imagine that you are working on a server and that when you initialize specific processes, you experience a crash. Many times, the Out of Memory error (OOM Killer as it's known) will occur when you try to start multiple core system processes simultaneously. This is especially common in lightly provisioned systems (virtual editions servers and the like). An easy fix for this is to allocate several Gb of swap memory. It's time to take a look at how to create swap memory. 

[NOTE: If you're still not convinced about swap, be sure to check out this great piece from Redhat.com]

How to swap?

There are two options when it comes to creating a swap space. You can create a swap partition or a swap file. Most Linux installations come preallocated with a swap partition. This is a dedicated block of memory on the hard disk utilized when the physical RAM is full. On the flip side, you can also create a swap file. This is a file that is designed to be a specific size and is best used only in particular circumstances. It is recommended to use a swap partition unless the situation dictates otherwise. 

I am going to demonstrate the procedure for extending swap on an LVM logical volume. You can follow along here, or see the customer portal for the full documentation, including a method for a non-LVM swap. 

[NOTE: To see the full documentation, please see the RHEL customer portal. ]

Before you create or increase swap, it is helpful to determine if your machine already has a swap available. You can quickly check this by running the free -m command. You can see here that a fresh installation of RHEL8 in Virtual Box comes preallocated with a 3Gb swap, which is not currently being utilized.

Image
output of free-m

Now, say that system utilization dictates that you need to increase the swap space available. You begin by disabling the swap for the associated volume. 

Disable: # swapoff -v /dev/rhel/swap
Resize:   # lvresize /dev/rhel/swap -L +2G (to increase swap by 2G)
Format:  # mkswap /dev/rhel/swap
Enable:  # swapon -v /dev/rhel/swap

If you need to reduce the swap space for any reason, you simply change step 2 to look something like this:  

Reduce: # lvreduce /dev/rhel/swap -L -1G (to reduce the swap by 1Gb)

More Swap?

There may be other things that you need to manipulate when it comes to swap. The creation of a new volume or complete removal of swap is sometimes necessary. There is also the swap file that I mentioned earlier. For more information on how to accomplish these tasks, be sure to check the link to the customer portal from earlier in the article.

[NOTE: For a great deep-dive on this topic, be sure to check out David Both's article over at Opensource.com]

Topics:   Linux  
Author’s photo

Tyler Carrigan

Tyler is a community manager at Enable Sysadmin, a submarine veteran, and an all-round tech enthusiast! He was first introduced to Red Hat in 2012 by way of a Red Hat Enterprise Linux-based combat system inside the USS Georgia Missile Control Center. More about me

Free Event: Red Hat Summit 2020 Virtual Experience

Attend the Red Hat Summit 2020 virtual experience, April 28-29.

Related Content

OUR BEST CONTENT, DELIVERED TO YOUR INBOX