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.
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]
About the author
Tyler is the Sr. 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. Now that he has surfaced, he lives with his wife and son near Raleigh, where he worked as a data storage engineer before finding his way to the Red Hat team. He has written numerous technical documents, from military procedures to knowledgebase articles and even some training curricula. In his free time, he blends a passion for hiking, climbing, and bushcraft with video games and computer building. He is loves to read and enjoy a scotch or bourbon. Find him on Twitter or on LinkedIn.
Browse by channel
Automation
The latest on IT automation for tech, teams, and environments
Artificial intelligence
Updates on the platforms that free customers to run AI workloads anywhere
Open hybrid cloud
Explore how we build a more flexible future with hybrid cloud
Security
The latest on how we reduce risks across environments and technologies
Edge computing
Updates on the platforms that simplify operations at the edge
Infrastructure
The latest on the world’s leading enterprise Linux platform
Applications
Inside our solutions to the toughest application challenges
Original shows
Entertaining stories from the makers and leaders in enterprise tech
Products
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Cloud services
- See all products
Tools
- Training and certification
- My account
- Customer support
- Developer resources
- Find a partner
- Red Hat Ecosystem Catalog
- Red Hat value calculator
- Documentation
Try, buy, & sell
Communicate
About Red Hat
We’re the world’s leading provider of enterprise open source solutions—including Linux, cloud, container, and Kubernetes. We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.
Select a language
Red Hat legal and privacy links
- About Red Hat
- Jobs
- Events
- Locations
- Contact Red Hat
- Red Hat Blog
- Diversity, equity, and inclusion
- Cool Stuff Store
- Red Hat Summit