Swap Partition Size
RedHat
RedHat distributions should have a swap partition size of 20% of the available RAM. The minimum amount of RAM is 4GB.
CentOS
CentOS provides the following guidelines:
The swap partition should be twice the amount of RAM if the total RAM amount is below 2GB.
If you have more than 2GB of RAM, then the swap partition should be the size of RAM + 2 GB. For example, use 8GB of swap for 6GB of RAM.
Ubuntu/Debian
Ubuntu has based its swap size recommendations on the use of hibernation.
If you use hibernation, the swap partition should be the same as the amount of RAM plus the square root of the RAM amount.
If you don’t use hibernation:
For less than 1GB of RAM, the swap partition size should be at least the same as the amount of RAM and, at most, double the amount of RAM.
For more than 1GB of RAM, create a swap partition at least equal to the square root of the RAM amount and, at most, double the RAM amount.
swapon Command
swapon –show
Check /proc/swaps File
cat /proc/swaps
Comman to check your swap
free -h
How to Create a Swap Partition
1. Run the fdisk command for the disk you want to partition:
sudo fdisk [disk_path]
Replace [disk_path] with your disk identifier.
2. Type p and press Enter for an overview of the disk.
The output provides information about the available disk space for a new partition. If the last partition ends before the last sector of the hard drive, there is enough space to create a new partition. The output above shows the entire disk space is available for creating a new partition.
3. Type n and press Enter to create a new partition. For partition type, enter p and press Enter.
4. Set the partition number (or keep the default number 1) and press Enter to confirm.
5. Enter the first available sector for the new partition and press Enter. You can also keep the default one offered.
6. Set the last available sector for the partition. You can specify the partition size in KiB, MiB, or GiB units. We will create a swap partition of 1GiB, so the command is:
+1G
7. Press p to verify the partition creation and confirm with Enter.
8. By default, the partition type is 83, while 82 is a partition type identifier for swap space.
Type t and press Enter to change the partition type. Change the value to 82 and press Enter.
9. Verify the change with p and confirm with Enter.
10. fdisk doesn’t make any changes to the disk before confirmation. To proceed with the changes, type w, and press Enter.
12. Update the partition table using the following syntax:
sudo partprobe [disk_path]
13. Use the mkswap command to format the partition for swap space. The command writes a swap signature in the partition, leaving the rest unformatted. The unformatted space stores the memory pages.
Use the following syntax to format the partition and create a swap space:
sudo mkswap [disk_path]
After formatting, the swap partition is created and ready to use.
How to Initialize/Enable a Swap Partition
3. Make Swap Persistent (Optional)
Include the partition in the /etc/fstab file to ensure the swap partition activates on boot.
vi /etc/fstab
Add the following line at the end:
/dev/sdb none swap sw 0 0
Replace /dev/sdb with the path to your swap partition.
How to Change the Size of a Swap Partition