Swap Expansion
Notes for checking and expanding swap on Linux/RHEL systems.
Swap is disk space used as extra memory when RAM is full. It is slower than RAM, but it can help prevent processes from failing when memory pressure is high.
Safety rule
Before changing swap, check:
1. Am I on the correct server?
2. Is this production or test?
3. Is the system actually running out of memory?
4. Is swap already configured?
5. Is the disk or filesystem where swap will be created healthy?
6. Is there enough free disk space?
7. Is the server using LVM or a swap file?
8. Do I have approval?
Do not use swap expansion as a replacement for fixing a real memory leak.
Check current memory and swap
Show memory and swap usage
free -h
Show active swap devices/files
swapon --show
Alternative:
cat /proc/swaps
Check disk space
df -h
Check block devices
lsblk
Option 1: Create a swap file
This is often the simplest method.
Example: create a 4 GB swap file.
1. Create swap file
sudo fallocate -l 4G /swapfile
If fallocate is not available or does not work:
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress
2. Set correct permissions
sudo chmod 600 /swapfile
3. Format as swap
sudo mkswap /swapfile
4. Enable swap
sudo swapon /swapfile
5. Verify
swapon --show
free -h
6. Make swap permanent
Edit /etc/fstab:
sudo nano /etc/fstab
Add:
/swapfile none swap sw 0 0
Test fstab:
sudo mount -a
Then verify swap:
swapon --show
free -h
Option 2: Extend existing LVM swap
Use this if swap is already on an LVM logical volume.
1. Check swap
swapon --show
lsblk
sudo lvs
sudo vgs
Example swap device:
/dev/mapper/vg_root-lv_swap
2. Turn swap off
sudo swapoff /dev/VG_NAME/LV_SWAP_NAME
Example:
sudo swapoff /dev/vg_root/lv_swap
3. Extend the logical volume
Example: add 4 GB:
sudo lvextend -L +4G /dev/VG_NAME/LV_SWAP_NAME
Example:
sudo lvextend -L +4G /dev/vg_root/lv_swap
4. Recreate swap signature
sudo mkswap /dev/VG_NAME/LV_SWAP_NAME
Example:
sudo mkswap /dev/vg_root/lv_swap
5. Turn swap back on
sudo swapon /dev/VG_NAME/LV_SWAP_NAME
Example:
sudo swapon /dev/vg_root/lv_swap
6. Verify
swapon --show
free -h
sudo lvs
Check swappiness
Swappiness controls how aggressively Linux uses swap.
Show current swappiness
cat /proc/sys/vm/swappiness
Alternative:
sysctl vm.swappiness
Temporarily change swappiness
Example:
sudo sysctl vm.swappiness=10
Permanently change swappiness
Create a config file:
sudo nano /etc/sysctl.d/99-swappiness.conf
Add:
vm.swappiness = 10
Apply:
sudo sysctl --system
Common values
vm.swappiness = 60 default on many systems
vm.swappiness = 10 common for servers where swap should be used less aggressively
Do not change swappiness blindly. Follow company standards.
Troubleshooting high swap usage
High swap usage can mean:
not enough RAM
memory leak
application using too much memory
too many processes
wrong application sizing
normal behavior after previous memory pressure
First checks:
free -h
swapon --show
top
ps aux --sort=-%mem | head
journalctl -k | grep -i "out of memory"
dmesg -T | grep -i "killed process"
OOM killer checks
If Linux killed a process because of memory pressure:
journalctl -k | grep -i "out of memory"
or:
dmesg -T | grep -i "killed process"
Disable swap temporarily
sudo swapoff /swapfile
or for a swap LV:
sudo swapoff /dev/VG_NAME/LV_SWAP_NAME
Remove a swap file
Only do this when you are sure it is no longer needed.
sudo swapoff /swapfile
sudo rm /swapfile
Then remove the /swapfile line from:
/etc/fstab
Verify:
swapon --show
free -h
First command set for swap issues
hostnamectl
free -h
swapon --show
cat /proc/swaps
df -h
lsblk
sudo lvs
sudo vgs
top
ps aux --sort=-%mem | head
journalctl -k | grep -i "out of memory"
Dangerous actions
Be careful with:
swapoff on systems under memory pressure
mkswap on the wrong device
editing /etc/fstab
removing swap files
changing LVM volumes
changing swappiness without approval
Running mkswap on the wrong device can destroy existing data.
Personal notes
Add sanitized notes here.
Examples:
- Swap was full because application memory usage increased.
- Swap file was added as a temporary fix.
- Real issue was a memory leak, not missing swap.
- LVM swap was extended after checking free VG space.