How to Change the SwapFile Size at Linux?


If you decide to change the swap file size, then following the steps:

Turn the Swap Off

1
swapoff -a
swapoff -a

Resize the current Swapfile

The following resizes the swapfile to 1GB (1M x 1024)

1
2
3
4
 dd if=/dev/zero of=/swapfile bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 4.22595 s, 254 MB/s
 dd if=/dev/zero of=/swapfile bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 4.22595 s, 254 MB/s

Make it a Swapfile

1
2
3
mkswap /swapfile
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=435cec7d-6c20-45c8-bd96-55e60ddf
mkswap /swapfile
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=435cec7d-6c20-45c8-bd96-55e60ddf

Turn back on the Swapfile

1
swapon /swapfile
swapon /swapfile

Confirm it is On

You can verify the size change by using command free -h or htop.

1
2
3
4
5
root@uploadbeta:/# free -h
             total       used       free     shared    buffers     cached
Mem:          2.9G       2.6G       316M       127M       148M       1.7G
-/+ buffers/cache:       842M       2.1G
Swap:         1.0G         0B       1.0G
root@uploadbeta:/# free -h
             total       used       free     shared    buffers     cached
Mem:          2.9G       2.6G       316M       127M       148M       1.7G
-/+ buffers/cache:       842M       2.1G
Swap:         1.0G         0B       1.0G

Add it to Startup

Make sure this is added to /etc/fstab so when system reboots, the swapfile is turned on automatically.

1
2
LABEL=root      /             ext4     defaults,errors=remount-ro 1 1
/swapfile       none    swap    sw      0       0
LABEL=root      /             ext4     defaults,errors=remount-ro 1 1
/swapfile       none    swap    sw      0       0

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
244 words
Last Post: How to Return a Default Weight for Products in Woocommerce shop?
Next Post: How to Automatically Restart MySQL Server in the event of Crash?

The Permanent URL is: How to Change the SwapFile Size at Linux?

Leave a Reply