How to change the size of your swap file?

What is it?

Reasons to change the size of the swap file

  • When your box ran out of spaces, decreasing swap file size is the quickest way that you can release some spaces, especially when you have a huge swap file.
  • When you have a server with lots of processes running on it, increasing swap file will let the kernel to move inactive processes into swap to make room for active processes in the working memory.

Step-by-step guide

  • Create a file named /tmp/ with script below
#!/bin/bash # don't do anything if we already have swap file settings if [[ $(swapon -s | wc -l) = 1 ]] ; then echo "Creating your swap file" dd if=/dev/zero of=/c66-swapfile bs=1024 count=$1k mkswap /c66-swapfile swapon /c66-swapfile echo "/c66-swapfile none swap sw 0 0 " >> /etc/fstab chown root:root /c66-swapfile chmod 0600 /c66-swapfile echo "Swap file successfully created" fi
  • Give executing permission to the script
sudo chmod +x /tmp/
  • Remove the current swap file
$ sudo swapoff -a $ sudo rm -f /c66-swapfile
  • Create a new swap file using script above
sudo /tmp/ <size_in_mb>



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cloud 66

Cloud 66

DevOps-as-a-Service to help developers build, deploy and maintain apps on any Cloud. Sign-up for a free trial by visting: