Tags

11g (3) 12c (4) 18c (2) 19c (3) ASM (3) Critical Patch (12) Dataguard (10) GRID (3) GitLab (2) Linux (11) OEM (2) ORA Errors (16) Oracle (20) RMAN (5)

Tuesday, June 16, 2026

How to create swap file

 

How to Create, Verify, and Increase Swap Space on Linux

Swap space is disk space that Linux uses as virtual memory when physical RAM is exhausted. Proper swap configuration helps improve system stability and prevents out-of-memory situations.

PURPOSE: All documents are provided on this Blog just for educational purposes only.  Please make sure that you run it in your test environment before to move on to production environment. 

What is Swap Space?

Swap is a reserved area on disk that acts as an extension of RAM. When memory usage becomes high, inactive pages can be moved from RAM to swap, freeing memory for active processes.

Benefits of Swap

  • Prevents application crashes due to memory shortages.
  • Provides additional virtual memory.
  • Supports system hibernation (when configured appropriately).
  • Improves system stability during memory spikes.

Check Existing Swap Space

Use any of the following commands to verify current swap configuration and usage.

free -h

Check Active Swap Devices

swapon -s

cat /proc/swaps

swapon --show

Create a New Swap File

This method creates a 2 GB swap file. Adjust the size according to your requirements.

Step 1: Disable Existing Swap (if required)

swapoff -a

Step 2: Create Swap File

sudo fallocate -l 2G /swapfile

Verify file creation:

ls -lh /swapfile

Step 3: Set Correct Permissions

sudo chmod 600 /swapfile

Verify permissions:

ls -l /swapfile

Step 4: Format the File as Swap

sudo mkswap /swapfile

Step 5: Enable Swap

sudo swapon /swapfile

Step 6: Verify Swap Activation

swapon --show

or

free -h

Step 7: Make Swap Persistent After Reboot

Add the following entry to /etc/fstab:

echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab

Verify:

cat /etc/fstab

Expected entry:

/swapfile none swap sw 0 0

Increase Existing Swap Partition

Current swap size: 8GB
Target swap size: 16GB

Step 1: Disable Swap

swapoff -a

Verify:

swapon --show

No output should be displayed.

Step 2: Increase Virtual Disk Size in vCenter

Increate hard disk 1 from 64 to 90 GB

Navigate to VMware vCenter:

VM Settings
→ Hard Disk 1
→ Increase disk size

Example:

64 GB → 90 GB

Step 3: Detect New Disk Size

Check available disk space:

fdisk -l

If the new size is not visible, rescan the disk:

echo 1 > /sys/class/scsi_device/0\:0\:0\:0/device/rescan

Run again:

fdisk -l

Step 4: Modify Partitions

Launch partition manager:

cfdisk

Existing Layout

/dev/sda3  Linux filesystem
/dev/sda4 Linux swap

Delete Existing Swap Partition

Select:

/dev/sda4

Choose:

Delete

Resize Root/Data Partition

cfdisk

Select:

/ dev/sda3  # This was a root partition which we increased on vCenter.

Choose:

Resize ==> Write

Create New Swap Partition

Use remaining free space:

New
Type → Linux swap

mkswap /dev/sda4 Setting up swapspace version 1, size = 26 GiB (27917262848 bytes) no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx # edit the UUID vi /etc/fstab mount -a swapon /dev/sda4 swapon --show NAME TYPE SIZE USED PRIO /dev/sda4 partition 26G 0B -2

No comments:

Post a Comment

How to create swap file

  How to Create, Verify, and Increase Swap Space on Linux Swap space is disk space that Linux uses as virtual memory when physical RAM is e...