How to Create and Remove Swap Memory in CLI on Oracle Linux 8.6

To Create and Remove Swap Memory on Oracle Linux 8.6

Introduction:

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM.

Procedure:

Step1: Check the OS version by using the below command

[root@linuxhelp linuxhelp]# cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="8.6"

Step 2: Check the available swap memory by using the below command

[root@linuxhelp linuxhelp]# top

Step 3: List the Disk by using the below command

[root@linuxhelp linuxhelp]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   19G  0 part
  ├─ol-root 252:0    0   17G  0 lvm  /
  └─ol-swap 252:1    0    2G  0 lvm  [SWAP]
sdb           8:16   0    5G  0 disk
sr0          11:0    1 1024M  0 rom  

Step 4: create partions on /dev/sdb disk by using the below command

[root@linuxhelp linuxhelp]# fdisk /dev/sdb


Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xa3db419a.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-10485759, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-10485759, default 10485759): +2G

Created a new partition 1 of type 'Linux' and of size 2 GiB.

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): l
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
Step 5: Format the partition /dev/sdb1 to the swap file system by using the below command
[root@linuxhelp linuxhelp]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=d83656e3-bd5c-4bdd-8cc7-3653a4c8001c

Step 6: Turn on the swap memory by using the below command

 [root@linuxhelp linuxhelp]# swapon /dev/sdb1

Step 7: Make fstab entry for swap partition by using the below command

 [root@linuxhelp linuxhelp]# vi /etc/fstab
/dev/sdb1  swap       swap    defaults     0 0

Step 8: Make permanent mounts of all entries in fstab by using the below command

[root@linuxhelp linuxhelp]# mount -a
mount: /mnt/raid0: can't find UUID=43b2f1de-bebe-412b-bc2a-645e86dfb8c5.

Step 9: Check the swap memory by using the below command


[root@linuxhelp linuxhelp]# top

Step 10: Turn Off the swap memory by using the below command

[root@linuxhelp linuxhelp]# swapoff /dev/sdb1

Step 11: Remove the fstab entries for /dev/sdb1 partition by using the below command

 [root@linuxhelp linuxhelp]# vi /etc/fstab

Step 12: Delete the /dev/sdb1 partition by using fdisk

 [root@linuxhelp linuxhelp]# fdisk /dev/sdb


Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Conclusion: We have reached the end of this article. In this guide, we have walked you through the steps required to Create and Remove Swap Memory in CLI on Oracle Linux 8.6 . Your feedback is much welcome.

FAQ
Q
When swap memory is used?
A
Swap memory is mainly used when physical memory or RAM is fully occupied, Sometimes to increase performance RAM or physical memory is better used for disk cache than to store program memory, So Swap Memory is also used when program data are transferred from Physical memory then the remaining free RAM or physical memory is effectively used for disk cache though it increases performance.
Q
How to update the partition table?
A
Update the partition table by using "partx –a" command.
Q
What is the HEX code of Snap?
A
The hex code for SWAP is 82.
Q
How to deactivate the swap?
A
To deactivate the swap use "swapoff " command.
Q
How to view available swap memory?
A
To view available swap memory use the command "swapon –s".