What is Swap partition How to use Swap partition on NVMe

NVMe is the next phase of the solid-state storage era, which improves the performance of SSDs and other storage devices on the network. Nowadays, more and more servers are starting to support NVMe, and it is time for IT personnel to consider the use of Swap partitions (swap areas) to make the most of these systems.

Before we look at the use of Swap partitions on NVMe disks, we need to understand what Swap partitions are and why we should avoid using them on SSDs. Then, this article will explore the issue of using Swap partitions on NVMe.

什么是Swap分区 NVMe上如何使用Swap分区

Photo by Fancycrave.com from Pexels

Requirements for Swap partitions

Swap partitioning is like fire insurance, it's something you never want to use, but it must be available at all times. On systems that run the way you want, there should be enough memory to load programs and cache data. If your system has enough RAM, then there is no problem.

The Swap partition will work when the system does not have enough RAM. A specific area dedicated to the hard disk acts as an additional RAM as an exchange area.

There are many reasons why a Linux system may need a swap area. It may be that there is not enough RAM, or it may be because some server-level applications (such as Oracle Database or SAP) require some swap space. In addition to this, there are some exceptions, such as a memory leak in your application server. In this case, you may be at risk of running out of memory, stopping the application, and dissatisfied customers.

Swap partition and SSD

On traditional mechanical hard drives, the use of Swap partitions has never been a problem. However, if the lifetime of the flash RAM unit you are using is limited, the situation is different. Every write on the flash wears the memory unit and at some point it stops working.

The expected life of an SSD device is represented by a TB Write (TBW) value, which indicates how many bytes can be written before the SSD is corrupted. The TBW value of an SSD device is usually listed as a persistent parameter in the product specification. The TBW value of a disk depends on its quality. Low-end consumer-grade SSDs can start to fail as low as 20 TBW, while enterprise/server-class SSDs can typically reach more than 1000 TBW. Limiting the use of Swap partitions can reduce the number of write operations on SSDs.

Swap partition use on NVMe

The main difference between a normal SSD device and an NVMe device is the way they are connected to the system bus. At the physical level, both device types use flash cells to write data, which means that there is no fundamental difference between the TBWs between SSDs and NVMes. But what does this mean for using Swap partitions on NVMe devices? This means that the rules that apply to non-NVMe SSDs also apply to NVMe SSDs.

I want to be clear: if the memory is not enough, and the Swap partition is always in use, then it is best to put it on the hard drive. However, this does not apply to typical Linux systems. Most Linux servers and workstations can handle the installed physical RAM well.

However, if the Linux kernel on these systems begins to swap memory pages to disk, it is important to see if the active or inactive memory pages are being swapped. Inactive memory pages are memory pages that have been allocated once and will not be used in the future. If your system only swaps inactive memory pages, then there is no need to worry, and there is no reason to avoid using Swap partitions on SSD or NVMe drives. This is because inactive memory pages are usually left in place after swapping out, which means that the amount of data written to the swap is much lower when only inactive pages are swapped out.

The best way to find out if your system is actively using Swap partitions is to run the vmstat command, which provides a system-wide view of performance, including processes, Swap usage, memory, paging, and CPU activity.

For example, if you run the vmstat 2 100 command, it will display 100 times the system uses Swap information at 2 second intervals. You need to take a closer look at the SI and SO columns of the Swap In and Swap Out results. The first line of vmstat output is a long-term summary that can be ignored. When the command generates output, you want to monitor what happens in the rest of these columns. If you don't see any important activities, then there is nothing to worry about. If you do see significant activity, then your SSD device is slowly depleting, and you might want to consider adding more RAM to your system.