Hello, and welcome to today's video presentation on using swap on Oracle Linux 8.
In this video, we will cover a brief background of swap space and how to configure it within
an environment.
So let us get started.
Oracle Linux 8, and other Linux systems, use swap to help free physical memory (RAM) when
it becomes exhausted.
The term "swap" or "paging" refers to the action of moving memory pages between RAM
and a pre-configured space on a disk.
Due to the swap space residing on a disk, access times to the memory pages is slower
and could result in degraded performance over time.
However, the kernel works to address this loss in performance by keeping a map of where
the swap space resides on disk and directly accesses it using disk block IO and bypassing
all caching, metadata, and filesystem code.
Since the use of swap space can decrease the performance of a system, is swap needed?
Despite this performance impact and the fact that most modern systems have substantially
more physical memory, it is still a good idea to configure swap.
A key benefit of having swap configured is that swap gives the system administrator time
to address low memory issues.
Without the swap configured, if an out-of-memory, or OOM for short, occurs, a series of severe
adverse reactions are likely to happen.
These reactions may include the kernel triggering the direct termination of critical system
processes and eventually causing a system crash.
Having covered some of the background information, let us jump into creating some swap space.
For this demo, the Oracle Linux 8 system used is an Oracle Cloud Infrastructure instance
with a free ext4 file system on /dev/sdb1.
The steps executed in this video also work with on-premise hardware configured with existing
block storage and a file system.
Start by first opening a terminal.
Configuring the swap space in Linux is either done as a regular file in the file system,
called a swap file, a separate partition, or a combination of swap partitions and swap
files.
Although a dedicated swap partition performs historically faster, a swap file is more convenient
for administrators to change and make adjustments.
This flexibility outweighs the speed advantage of swap partitions.
To determine the type of swap space currently available on the system, type sudo swapon
--summary, or use the -s option for short.
The output shows the existing swap space type as a file, with a size of 8GB, and located
at /.swapfile.
The same information is available from the proc file system by typing sudo cat /proc/swaps.
If the system happened to use a dedicated swap partition rather than a swap file, the
type would appear as the type partition, and the filename would list the name of the actual
partition used on the block device.
With the current swap space known, create additional swap space for the system.
Create a 1 GB swap file by typing sudo dd if=/dev/zero of=/.swapfile2 bs=1024 count=1048576
Next, prepare the swap file by typing the command sudo mkswap /.swapfile2
If this was a swap partition, pass the partition name of the block device to mkswap after creating
the partition with fdisk.
Lastly, enable the new swap file by typing sudo swapon /.swapfile2.
Be sure to add the new swap file to the /etc/fstab if needing the additional swap space after
a reboot.
The entry in /etc/fstab will match the callout on the screen.
With the new swap space created, time to check the systems resources.
To see the amount of free and used system memory, type sudo free -h.
The -h option displays the output in a human-readable format and shows the system is not currently
using any of the swap available.
Beyond the free command, several other tools exist to provide useful memory and swap information.
Such tools include top, htop, glances, and vmstat, to name a few.
Information also exists from the proc file system by typing sudo cat /proc/meminfo.
If a specific tool does not exist on the system, search for and install it from the Oracle
Linux repositories using dnf.
With the swap space created and a list of tools to monitor its use, now it is time to
review a kernel parameter to tune its use.
Added in the Linux 2.6 kernel, developers created a new parameter called swappiness
to allow administrators to tweak the way Linux swaps.
The setting for swappiness ranges from 0 to 100, with a default value of 60.
In principle, higher values lead to more pages transferred to disk, and lower values lead
to more being kept in memory, even if idle.
While the default value works well in most workloads, lowering the setting could be appropriate
for desktops and interactive systems with more than adequate RAM to complete any expected
tasks.
In contrast, less interactive systems, such as those performing batch processing, may
wish to increase it.
To alter the setting temporarily (until the next reboot), type echo "50" | sudo tee /proc/sys/vm/swappiness.
If the goal is to permanently alter the setting, change or add the vm.swappiness and value
in the /etc/sysctl.conf file.
To have the setting in /etc/sysctl.conf take effect immediately, type sudo sysctl -p.
The last part of this exercise is to undo all of these changes.
Start by turning off the new swap file by typing sudo swapoff /.swapfile2.
Alternatively, typing sudo swapoff -a will turn off all the swap on the system.
Lastly, remove the entry from the /etc/fstab, if created, and delete the file .swapfile2.
That completes this exercise of creating and using swap space.
As a system administrator, managing swap space is a fundamental aspect of the job and provides
many benefits with the right planning and proper use.
Monitor systems for excessive swapping, also known as thrashing.
When discovering this behaviour, evaluate adding more physical memory, not more swap
space.
Do not consider increasing swap space as a solution to memory shortages, but rather a
safety net when physical memory runs out.
Thank you for watching this video and enjoy using Oracle Linux 8.
Please check our additional information on Oracle Linux 8 in the Oracle Linux 8 Documentation
or Oracle Linux Learning Library.