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.

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]


关于作者

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.

UI_Icon-Red_Hat-Close-A-Black-RGB

按频道浏览

automation icon

自动化

有关技术、团队和环境 IT 自动化的最新信息

AI icon

人工智能

平台更新使客户可以在任何地方运行人工智能工作负载

open hybrid cloud icon

开放混合云

了解我们如何利用混合云构建更灵活的未来

security icon

安全防护

有关我们如何跨环境和技术减少风险的最新信息

edge icon

边缘计算

简化边缘运维的平台更新

Infrastructure icon

基础架构

全球领先企业 Linux 平台的最新动态

application development icon

应用领域

我们针对最严峻的应用挑战的解决方案

Virtualization icon

虚拟化

适用于您的本地或跨云工作负载的企业虚拟化的未来