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.
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.
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 runsudo bash
before running the commands.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:
If you want to make a 3GiB file, then change count value to
count=3M
. Seeman dd
for more information.Bake the swap file:
The following command is going to make a "swap filesystem" inside your fresh swap file.
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.Activate:
You can either reboot your computer or activate the new swap file by hand with the following command:
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:
Resize Swap to 8GB
Check if it worked
Make it permanent (persist on restarts)
Add this line to the end of your
/etc/fstab
:GUI method for increasing the size of swap partition
Another way to increase the swap size is to use the GParted partition Editor. In short, you resize the swap partition, then right click on it and choose "Swapon".
It's easier to boot gparted-live-disk or an Ubuntu live disk (so that the all
/dev/sda
partitions will be unmounted). If you run Ubuntu live disk in some versions before 18.04, you may need to install gparted by running the commands below:You must be able to increase the size of swap partition only if there is an unallocated space present before or after the swap partition. If there was no unallocated space (the space which we are trying to add with swap partition) below or above the swap partition, then we have to resize the partitions and get that unallocated space.
Case 1 - unallocated space present before or after the swap partition
In the above screenshot, I had 11.4 GB of unallocated space before the linux-swap and 12.8 GB after the swap partition.
To resize, right click on the swap partition (/dev/sda9 here) and click on the Resize/Move option. It will look like this:
Dragging the slider arrows left or right then click on the Resize/Move button. Your swap partition will be resized.
Case 2 - unallocated space is between the partitions
In the above screenshot, the unallocated space which we wants to add to the swap partition was between
dev/sda7
and/dev/sda8
.To move the unallocated space from that to just above swap partition,we have to follow the simple steps given below,Right-click on the /dev/sda8 partition and click on Resize/Move option.
click and move the slider to the extreme left,so that the unallocated space which was just above the
/dev/sda8
partition will comes below/dev/sda8
.After that click Resize/Move button.And now the unallocated space was just above to the swap partition which was like Case 1.Now follow Case 1.
Case 3 - if the unallocated space was present outside the Extended partition)
I had an unallocated space of 18 GB just below to the extended partition.To add this space to the linux-swap partition(which was present inside the extended partition),we have to follow the below steps,
Right-click on the extended partition and select Resize/Move option,it will be like the below screenshot
Click and drag the arrow to the extreme right and click on Resize/Move,so that the unallocated space of 18 GB will comes at the bottom of extended partition.I had a 14.80 GB of unallocated space already present at the bottom and now the 18.34 GB combines with that to create unallocated space of (18.34+14.80 GB) at the bottom of the extended partition.
Now there was an unallocated space just below to the swap partition,it will be like Case 1,then follow case 1.
NOTE: Don't forget to take backup of all your important datas before proceeding the above operations.
You can also use
fallocate
if you want to reserve space for your swapfile, without the need to fill the file with 0 throughdd
.From the
man
page:If you already have a
swap
defined, you can either remove it or keep it, and your *nix will manage it (even better with by defining aswapon
priority). This could be usefull, in case you want to spread your swap between different devices, drive (based on their speed or other custom needs, see What is the purpose of multiple swap files on StackExchange).Simple
fallocate
usage for adding a second swap fileCheck swap situation:
Create a 4G swap file
Standard way of creating and activating a swap file:
One-liner:
Make your swap permanent
Add a line in your
/etc/fstab
file so that swap will be initialized on the next reboot (we also update here the priority of the newly created swap space and we update the priority of the old swap file).In your /etc/fstab file, notice the priority of the prev swap is now 10.
Check swap situation after reboot:
Remove one of the swap spaces (for example the original 256Mb)
Edit
/etc/fstab
Remove the line related to the swap you want to remove.
Delete that swap
Delete and remove the swap file.
Resources:
In my case I already had a swap file which I thought was 1GB but when I looked at it more closely it was only 256 MB big and when pre-compiling my rails assets the server would run out of memory
I created my swap file as described here: https://stackoverflow.com/a/34885004/784318
Now the line that creates an empty file called
/swapfile
is as follows:Now to resize the swapfile I had to do these simple steps:
The file was created (536870912 / 1024 / 1024 = 512 MB):
Using
free -m
command I could see that the file now is 512 MB like expected:I found the following video very interesting and a straightforward approach to increase the size of swapfile. Checkout this short video tutorial: How to Increase Swap on Ubuntu Linux | Linux Handbook
(From this video) following is the way to increase the swap space (if your system is using swapfile):
First check the swap size of your linux system:
Sample output:
This output shows the swap size is being used and the type of swap. Here the swap type is "file" type (it could be partition type as well).Ubuntu uses
swapfile
by default and increasing and decreasing the size of it is fairly easy.As we are going to modify the
swapfile
so at first turn it OFF. You might notice the process is being killed for multiple times. This happens because before turning the swap OFF the system moves its contents to main memory which may overflow the main memory. So, if the following command is killed, run it multiple times until it is successfully completed. You may also face sudden logout and / or other processes may be killed automatically.:Now if you run
swapon --show
it won't show anything as it is turned OFF.Now change the swap size (I am making it 4GB, for n GB make it nG):
Now if you check the file you will see the size is increased. To see it:
Now tell the Linux system to use this file as swap system:
This will wipe the old swap signature and use the new one.
Now to turn the swap ON (as we initially turned OFF the swap space):
Done!
Now use
swapon --show
orfree -h
to see the change.If you want to resize your swap partition and do not want to create a swap file, then
Boot Ubuntu installation media live session by choosing Try Ubuntu
Open GParted or other partition manager if you use Kubuntu, Lubuntu, etc.
Disable swap:
Resize main partition (In my case I was doing it for
/dev/sda2
) by shrinking itResize swap partition making it bigger
Enable swap using the same context menu and Swapon button
Reboot into usual Ubuntu session
Open terminal and see if swap size changed:
It's not changed. To change it, do the next:
Disable swap:
Recreate swap:
Enable swap:
Check its size:
Check your swap partition's UUID:
Replace UUID in
/etc/fstab
file for swap partition by correct one:I've just commented older record and created a new record with the correct UUID from
blkid
command output.How to add a swap file
Create .img file
note!: bs=1M count=1000 ==> 1GB
(bs * count = size in megabytes )
Other example:
Format .img file
Enable swap file
Add swap file to fstab
Add this line to your fstab (/etc/fstab):
I have zfs and this worked for me.
sudo zfs create -V 8G -b $(getconf PAGESIZE) -o logbias=throughput -o sync=always -o primarycache=metadata -o com.sun:auto-snapshot=false rpool/swap
sudo mkswap -f /dev/zvol/rpool/swap
sudo swapon /dev/zvol/rpool/swap
In
/etc/fstab
I added this line (with tabs as spaces):/dev/zvol/rpool/swap none swap discard 0 0