Based on personal experience, a swap file might be better utilized than a swap partition in newer versions of Ubuntu. You could try creating a swap file and disabling the swap partition and see how it works for you. To do so, please follow these steps:
Create the swap file ( 8 Gib ):
sudo fallocate -l 8G /swapfile
Give the file right permissions to limit unneeded access:
sudo chmod 600 /swapfile
Prepare the file as a swap area:
sudo mkswap /swapfile
Activate the swap file:
sudo swapon /swapfile
Deactivate the swap partition:
sudo swapoff /dev/sda2
Test how your system swap behaves now.
Notice: ( except for the creation of the /swapfile
), these changes made to swap volumes are temporary and will be cleared after reboot. If you wish to make changes permanent,
please edit your /etc/fstab
file
sudo nano /etc/fstab
Add this line to the end of the file /swapfile swap swap defaults 0 0
example:
/dev/sda2 none swap sw 0 0
/swapfile swap swap defaults 0 0
Comment out the line with the swap partition ( /dev/sda2 ) by adding #
before it
example:
# /dev/sda2 none swap sw 0 0
/swapfile swap swap defaults 0 0
Save the changes by pressing Ctrl + X then Y then Enter
fallocate
or dd
:
Thank you to @heynnema for raising this concern in the comments section below:
no, don't use fallocate
to create /swapfile, use dd
. Search for
info about this here on AU.
I assume the concern is regarding the possibility of fallocate
creating file holes which basically are portions of a file that contain null characters and are not stored in any data block on the disk. This in turn, if present, would render the swap file unusable.
To address this concern, let us have a look at the odds of fallocate
creating file holes and the consequences of that.
It turns out:
fallocate
will not normally create holes unless it is operated on a preexisting file and the option --punch-hole
is specified. Please read Ubuntu Manpage - fallocate
fallocate --length 8GiB swapfile
is suggested to create a swap file on Ubuntu man pages, please read Ubuntu Manpage - mkswap
After all, if the created file contained holes in it, sudo swapon
will throw an error skipping - it appears to have holes.
and in this rare case which I haven't faced or heard of yet, the solution is simple. Just use dd
to create the file in step # 1 above and move on. Do it as follows:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=8388608
or as @v_mil did it:
sudo dd if=/dev/zero of=/swapfile bs=1048576 count=8192
In short, this concern is not worrying enough to give up on the speed gained by using fallocate
.
sudo sysctl vm.swappiness
to your question. – Raffa Aug 13 '19 at 16:21sudo sysctl vm.swappiness=10
and see how swap works afterwards. This will temporarily change the swappiness value until you reboot. If it works and you wish to make it permanent, please see this answer https://askubuntu.com/a/103916/968501 – Raffa Aug 13 '19 at 16:35