DEV Community

Bogdan Cornianu
Bogdan Cornianu

Posted on • Originally published at bogdancornianu.com on

Change swap size in Ubuntu 18.04 or newer

[Updated July 26, 2020]: Change swapfile permission; Set swapfile in /etc/fstab.

Swap is a special area on your computer, which the operating system can use as additional RAM.

Starting with Ubuntu 17.04, the swap partition was replaced by a swap file. The main advantage of the swap file is easy resizing.

Note: before running the following commands, please make sure you have a backup of your data!

In the following example, we’ll extend the swap space available in the /swapfile from 4 GB to 8 GB.

  • Turn off all swap processes
sudo swapoff -a
Enter fullscreen mode Exit fullscreen mode
  • Resize the swap
sudo dd if=/dev/zero of=/swapfile bs=1G count=8
Enter fullscreen mode Exit fullscreen mode

if = input file

of = output file

bs = block size

count = multiplier of blocks

  • Change permission
sudo chmod 600 /swapfile
Enter fullscreen mode Exit fullscreen mode
  • Make the file usable as swap
sudo mkswap /swapfile
Enter fullscreen mode Exit fullscreen mode
  • Activate the swap file
sudo swapon /swapfile
Enter fullscreen mode Exit fullscreen mode
  • Edit /etc/fstab and add the new swapfile if it isn’t already there
/swapfile none swap sw 0 0
Enter fullscreen mode Exit fullscreen mode
  • Check the amount of swap available
grep SwapTotal /proc/meminfo
Enter fullscreen mode Exit fullscreen mode

The post Change swap size in Ubuntu 18.04 or newer appeared first on Bogdan Cornianu.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay