This article explains how to configure Swap Space in Linux. It is important for managing memory efficiently during heavy workloads. Swap space acts as a backup for physical RAM and can be set up as a swap file or a dedicated partition.
Follow the below steps to configure swap space:
- Check existing Swap Space before adding it:
- Show swap details:
$ sudo swapon --show
- View overall memory usage:
$ free -h
If no output is returned, swap space is not configured.
- Show swap details:
- Creating a Swap File
To add swap space without changing partitions, follow these steps:- Create the Swap File:
$ sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
Replace 1024 with the desired size in MB (e.g., 2048 for 2GB).
- Set File Permissions:
$ sudo chmod 600 /swapfile
- Format the File as Swap:
$ sudo mkswap /swapfile
- Activate the Swap File:
$ sudo swapon /swapfile
- Make it Permanent: Add this line to /etc/fstab:
/swapfile none swap sw 0 0
- Create the Swap File:
- Configuring a Swap Partition
For long-term performance, a dedicated swap partition is often chosen:- Identify or Create a Partition:
List available partitions using:$ sudo fdisk -l
Create a new partition using fdisk or a similar tool, and set its type to Linux swap (type 82).
- Format the Partition:
$ sudo mkswap /dev/sdX
Replace /dev/sdX with your partition’s identifier (e.g., /dev/sda3).
- Activate the Swap Partition:
$ sudo swapon /dev/sdX
- Make it Persistent: Add this line to /etc/fstab:
/dev/sdX none swap sw 0 0
- Identify or Create a Partition:
- Adjusting Swap Settings
- Check current “swappiness” value (controls swap usage frequency):
$ cat /proc/sys/vm/swappiness
- Temporarily adjust swappiness:
$ sudo sysctl vm.swappiness=10
- For permanent changes, add this to /etc/sysctl.conf:
swappiness=10
- Check current “swappiness” value (controls swap usage frequency):
This way, you can configure swap space as it ensures stable performance, especially for memory-intensive tasks or servers. If you need assistance, feel free to contact our support staff.
Running low on space? Learn How to increase disk space for a domain in Linux reseller account