How to Increase Swap Size in Linux: A Complete Guide

Introduction

Managing swap space efficiently is essential for optimal Linux performance, especially when handling memory-intensive applications. Sometimes, Oracle databases or Oracle Enterprise manager or any other application which is asking for swap will fail installations. This guide covers three common scenarios to Increase swap size in Linux:

  1. When no swap is available.
  2. When swap is already available but needs expansion.
  3. When swap is created using a file.

Follow these step-by-step instructions to optimize your system’s performance with proper swap management. We will create 4G swap space

Checking Current Swap Space

Before modifying swap, check the existing swap space:

sudo swapon --summary

command execution summary of swapon --summary

or

free -h
command execution summary of free -g

If no swap is available, the output will show 0B under the swap column.

Case 1: Adding Swap When No Swap is Available

Step 1: Create a Swap File

sudo fallocate -l 4G /swapfile

If fallocate is not available, use:

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

Please be aware that Before creating this file, you should check the storage available on root file system (/). If no storage available, you may need to Add/extend the storage.

Step 2: Set Permissions

sudo chmod 600 /swapfile

Step 3: Format the File as Swap

sudo mkswap /swapfile

Step 4: Enable the Swap File

sudo swapon /swapfile

Step 5: Make Swap Permanent

Add the following line to /etc/fstab:

/swapfile none swap sw 0 0

Case 2: Increasing Existing Swap Space

Step 1: Turn Off Current Swap

sudo swapoff -a

Step 2: Resize or Add More Swap Space

Option 1: Extend Swap Partition (If Using a Partition)

  • Use tools like fdisk, parted, or gparted to resize the swap partition.
  • Run sudo mkswap /dev/sdX (replace sdX with your swap partition).

Option 2: Create an Additional Swap File

If modifying partitions is not feasible, create another swap file (refer to Case 1 steps) and enable it:

sudo swapon /swapfile02

Step 3: Verify Swap Space

free -h

Case 3: Creating Swap Using a File

Step 1: Allocate Swap Space

sudo fallocate -l 4G /swapfile

We will use “swapfile” file name for this purpose

Step 2: Secure Swap File

sudo chmod 600 /swapfile

Step 3: Convert to Swap Format

sudo mkswap /swapfile

Step 4: Enable Swap

sudo swapon /swapfile

Step 5: Make It Persistent

Edit /etc/fstab and add:

/swapfile none swap sw 0 0

Optimizing Swap Performance

To adjust swappiness (how often swap is used), modify the value in /etc/sysctl.conf:

echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

Conclusion

Increase swap size in Linux can improve system performance and prevent out-of-memory errors. Whether adding swap for the first time, expanding existing swap, or using a file-based swap, following these steps ensures your Linux system runs smoothly.

Related Topics

  • How to Monitor Swap Usage in Linux
  • Best Practices for Linux Memory Management
  • Understanding Swappiness in Linux

Let us know in the comments if you have any questions!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.