Skip to main content

SWAP

====================================================================================

SWAP

What is SWAP?

Swap memory is space that is reserved via the hard disk and should only be utilized when necessary and in cases where the servers dedicated memory has been mostly used up.

The swappiness value is actually a kernel tunable - see here for more information

How does a system decide how often to use SWAP over physical memory?

In Linux, the /proc/sys/vm/swappiness file is a kernel parameter that controls the swappiness behavior of the virtual memory system. Swappiness is a setting that determines how aggressively the kernel will use swap space.

Understanding Swappiness

Swappiness Value: The value of swappiness can range from 0 to 100.

0: The kernel will avoid swapping out processes as much as possible, preferring to keep data in RAM.

100: The kernel will aggressively swap processes out of physical memory and move them to swap space, keeping more RAM free.

Swappiness at 0:

The kernel will prioritize keeping processes in RAM, only swapping out data when absolutely necessary.

This setting is useful for systems where you want to minimize latency and keep performance high for active applications, such as on a desktop or server where response time is critical.

Swappiness at 100:

The kernel will swap out data more readily, even if there is still available RAM.

This setting can be useful in certain scenarios where you want to maximize the use of RAM for disk caches and reduce the amount of cached data being cleared to make room for process memory.

------------------------------------------------------------------------------------------------------------------------------------------------

Managing SWAP configuration

Check swappiness value
cat /proc/sys/vm/swappiness

Changing swappiness

Since swappiness is a kernel tunable, the only method to make a swappiness value change persist a reboot is to create a custom configuration in /etc/sysctl.d. I'll also detail the steps below:

Change swappiness value (persistent)

Create a new custom configuration file in /etc/sysctl.d

vim 00-custom-swap.conf

Within this file, place the following string (with your desired value added)

vm.swappiness=10

Once created, apply the settings using the below command:

sysctl -p
Change swappiness value (not persistent)

To change the swappiness value on a machine for the current session (doesn't persist reboot), we need to manually update the swappiness file:

vim /proc/sys/vm/swappiness

The file should contain only a number - this being the swappiness value. Change this number to your desired value. For example, to set a swap value of 60:

root@test:~# cat /proc/sys/vm/swappiness
60

Once the swappiness value has been changed, we then need to switch off/switch on swap to pull in the new value:

swapoff -a
swapon -a

------------------------------------------------------------------------------------------------------------------------------------------------

SWAP partitions

For SWAP to work in Linux, there has to be sections of a disk partitioned specifically for this purpose. By default, most Linux distros will come with a swap file/partition pre-created.

Check the current swap partition configuration
swapon --show
Create and enable a swap partition:
mkswap /dev/devicename
swapon /dev/devicename

====================================================================================