Introduction

Well you want to expand your swap file? Not have enough money to buy an extra RAM? Well, you are in the right place, In this blog I will guide you through the steps of expanding your preexisting swap file by successive 1 GiB or more!


Steps to increase swap file size

  • Disable swap file

We use swapoff command to turn off the swap since we are modifying the swap file, It will take some time depending upon the data in swap

sudo swapoff /swapfile
  • Append 1 GiB to the end of swapfile

By executing the below command, 1 GiB will be added at the end of the swap file, you can run more than once to add more swap memory.

sudo dd if=/dev/zero of=/swapfile bs=1M count=1024 oflag=append conv=notrunc

Explanation of above command :

  1. dd a command line tool used for creating and copying data
  2. if argument to specify the input file
  3. /dev/zero its a special file in linux, when read gives the values as 0
  4. of argument to specify the output file
  5. bs argument to specify the block size
  6. count number of blocks needed to be written, since we selected 1M, it will run 1024 times to give 1024M (1Mx1024)
  7. oflag=append tells the dd to append to the swap file.
  8. conv=notrunc This option tells dd not to truncate the output file to zero length before writing.
  • Create the swap file with updated size

The command below setup the swap file.

sudo mkswap /swapfile
  • Turn on the new swap file

The command below turns on the swap file so if needed applications might use it.

sudo swapon /swapfile

Summary

So for a quick summary, here are the commands to expand your swap by 1 GiB.

sudo swapoff /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024 oflag=append conv=notrunc
sudo mkswap /swapfile
sudo swapon /swapfile

Conclusion

System monitor

I increased my swap by running the dd command twice so 2 GiB will be appended on my existing swap resulting in 2.5 GiB.

Nice! You have increased your swap file! Now go open a few extra tabs! have fun! Have any questions? well comment below! I will try to hit you up as soon as possible!