How to Increase Swap Memory on Linux
Create, enable, and permanently configure swap memory on Linux servers with step-by-step commands.
Before you start
Step-by-step instructions
1. Create directory for swap file
Create a dedicated directory to store the swap file.
mkdir /swap
2. Create 1GB swap file
Create a 1GB swap file using dd command.
dd if=/dev/zero of=/swap/swapfile bs=1024 count=1048576
count value controls size. This example creates about 1GB swap.
3. Move to swap directory
Navigate to the directory where the swap file is created.
cd /swap
4. Create swap area
Convert the file into a usable swap memory area.
mkswap swapfile
5. Activate swap file
Enable the swap file so the system can start using it.
swapon swapfile
6. Verify swap configuration
Check whether the swap memory is properly applied.
swapon -s
If swapfile is listed, the setup is complete.
Common issues
If the server does not have enough free storage, the swap file cannot be created successfully.
This usually means the /etc/fstab entry was not added or was written incorrectly.
Swap can help prevent memory crashes, but it is much slower than real RAM. If memory pressure is constant, adding actual RAM may be the better solution.