DEV Community

Stack All Flow
Stack All Flow

Posted on • Originally published at stackallflow.com on

How to increase swap space in Ubuntu?

swap

I have, by default, 250MB of swap space in Ubuntu, and I want to expand it to a larger size. I need 800MB, which I think will be enough to open several applications without having to hit the current limit of swap. I hope someone can help me.

Accepted Answer

You can always create a swap file to add more swap space. This is not the same in every aspect as swap partition, but it will be easy and dynamic.

In the following steps, change /media/fasthdd/swapfile.img to anything you like. For example, it can be /swap.img as well. /media/fasthdd/swapfile.img is just an example filename. If you are using this one, then of course there must be a directory /media/fasthdd/ with enough free space for your new swap file.

Use any terminal application to run the commands of the following steps. All commands should be run _ with root privileges _. To do this, you can either add sudo to the beginning of every command or run sudo bash before running the commands.

  1. Create an empty file :

This file will contain virtual memory contents so make file big enough for your needs. This one will create a 1GiB file, which means +1GiB swap space for your system:

dd if=/dev/zero of=/media/fasthdd/swapfile.img bs=1024 count=1M

Enter fullscreen mode Exit fullscreen mode

If you want to make a 3GiB file, then change count value to count=3M. See man dd for more information.

  1. Bake the swap file :

The following command is going to make a “swap filesystem” inside your fresh swap file.

mkswap /media/fasthdd/swapfile.img

Enter fullscreen mode Exit fullscreen mode
  1. Bring up on boot :

To make sure that your new swap space is activated while booting up computer, you should add it to the filesystem configuration file /etc/fstab. Add it to the end of the file. This is recommended because other filesystems (at least one that contains a swap file) must be mounted in read-write mode before we can access any files.

# Add this line to /etc/fstab
/media/fasthdd/swapfile.img swap swap sw 0 0

Enter fullscreen mode Exit fullscreen mode
  1. Activate :

You can either reboot your computer or activate the new swap file by hand with the following command:

swapon /media/fasthdd/swapfile.img

Enter fullscreen mode Exit fullscreen mode

If everything goes well , you should see that more swap space is available for use. You can use the following commands to check your new swap and confirm that it is active:

$ cat /proc/swaps
Filename Type Size Used Priority
/media/fasthdd/swapfile.img file 8388604 2724 -1

$ grep 'Swap' /proc/meminfo
SwapCached: 4772 kB
SwapTotal: 8388604 kB
SwapFree: 8355812 kB

Enter fullscreen mode Exit fullscreen mode

The post How to increase swap space in Ubuntu? appeared first on Stack All Flow.

Discussion (0)