In the previous article, we talked about the method of adding swap area on Linux machine. So, it makes sense to thinking about how to remove it.
Let's begin with listing all active swap spaces.
$ free -ght
total used free shared buff/cache available
Mem: 892Mi 372Mi 75Mi 31Mi 444Mi 337Mi
Swap: 1.0Gi 209Mi 814Mi
Total: 1.9Gi 582Mi 889Mi
$ swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 1024M 209.6M -2
There are 209.8MiB of swap space currently in use and we have 337MiB of available physical memory. So, we have enough main memory to accommodate all inactive pages in swap area. Then, before we issue the swapoff
command to disable the swap space, we open another terminal window on the same machine and execute vmstat
command to monitor virtual memory.
$ vmstat -w 1
The option w
enables wide mode, which is particularly useful for machines with large amount of memory, where big numbers can cause column misalignment. The argument 1
sets the polling interval to be 1 second.

Then, we issue swapoff
command. This will instruct kernel to swap out everything currently in swap area into the main memory.
$ sudo swapoff /swapfile
After issuing the command, the terminal will be frozen. Meanwhile, there are massive I/O and swap-in operations happening behind the hood, which is captured by vmstat
.

When si
and bi
fields become 0, swapoff
command finishes in the other terminal. Finally, we can verify that the swap file has been deactivated by running free
and swapon --show
commands .
$ free -gth
total used free shared buff/cache available
Mem: 892Mi 505Mi 73Mi 43Mi 313Mi 192Mi
Swap: 0B 0B 0B
Total: 892Mi 505Mi 73Mi
$ swapon --show
At this point, we have already removed swap space from the system. However, it will be mounted again in next boot. So, it's also important to remove the corresponding entry from /etc/fstab
file.
Comments NOTHING