Managing and Configuring Swap Memory on Linux

Initial State Verification

Evaluate current swap utilization using free -h. Prior to making modifications, deactivate existing swap instances to prevent conflicts.

swapoff -a

Constructing the Swap File

Allocate disk space for swap funcsionality. Execute the following command to generate a 4GB file named swapfile within /opt.

dd if=/dev/zero of=/opt/swapfile bs=1M count=4096

Key parameters:

  • if: Specifies the input stream (typically /dev/zero).
  • of: Defines the output destination path.
  • bs: Sets block size in bytes.
  • count: Determines total blocks copied.

Convert this raw file into executable swap space.

mkswap /opt/swapfile

Configuring Kernel Behavior

Control memory pressure response using vm.swappiness. Query current values with:

sysctl vm.swappiness

Define the desired behavior in /etc/sysctl.conf. Lower numbers prioritize physical RAM usage over swapping out.

vm.swappiness = 10

Implementing Configuration

Load new sysctl parameters without rebooting. Subsequently, activate the newly created swap partition.

sysctl -p
swapon /opt/swapfile

Securing Mount Persistence

Update /etc/fstab to ensure swap remains active across reboots. Append a line specifying the file and its attributes.

/opt/swapfile swap swap defaults 0 0

Using UUIDs is recommended for robustness against device name changes.

UUID=<uuid-string> swap swap defaults 0 0

Field Definitions

  1. Device Identifier: Can be a path, label, or UUID.
  2. Target Mount: Should be set to swap.
  3. File System: Denoted as swap.
  4. Flags: Typically defaults applies standard read/write and execution permissions.
  5. Backup Frequency: 0 indicates no dump backup required.
  6. FSCK Order: 0 skips consistency checks at boot.

Tags: Linux swap memory-management system-administration filesystem

Posted on Sat, 25 Jul 2026 16:37:28 +0000 by mrherman