Adding Swap to Google Compute Engine

If you find this free website useful – why don’t you support this with a donation? It is easy…. read more ….

By default Google Compute Engine (Debian) doesn’t come with any swap space.  If you have periodic programs that eat a bit more memory (like my backups do) then instead on just adding more physical memory you can add a swap file.

Swaps files are much slower than physical memory, so should be avoided except when it is needed.

Setting up a swap file on Debian on Google Compute Engine is described here

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon -s

To make this permanent add this to /etc/fstab

/swapfile none swap sw 0 0


If you need to resize the swap file

sudo swapoff /swapfile 
sudo fallocate -l 4G /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile


How much swap? Well that  is down to your system. Maybe start with 1x your physical memory and then run memory diagnostics tools.









One response to “Adding Swap to Google Compute Engine”

  1. Rick avatar

    Thanks. Normally, this gets set up when you create a new system, so you don’t think about it. Setting up a gcloud debian VM didn’t prompt for it, so it didn’t get set up.

    This saves the day on a minimal gcloud compute instance – lets me keep it a bit longer before upgrading.

Leave a Reply

Your email address will not be published. Required fields are marked *