I have tablet with 4 GiB of RAM and SSD 128 GiB with LUbuntu 18.04.
When the memory usage reaches approx. 3.9 G, the system hangs. Only the mouse pointer moves with lags. No reaction on Ctrl+Alt+Fx.
swapon
is reported 8 GiB:
$ sudo swapon --show
NAME TYPE SIZE USED PRIO
/dev/sda2 partition 8G 0B -2
$ sudo sysctl vm.swappiness
vm.swappiness = 60
$ uname -r
4.15.0-55-generic
Why the memory is not actually swapped? How to enable swap?
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 ):
Give the file right permissions to limit unneeded access:
Prepare the file as a swap area:
Activate the swap file:
Deactivate the swap partition:
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
fileAdd this line to the end of the file
/swapfile swap swap defaults 0 0
example:
Comment out the line with the swap partition ( /dev/sda2 ) by adding
#
before itexample:
Save the changes by pressing Ctrl + X then Y then Enter
fallocate
ordd
:Thank you to @heynnema for raising this concern in the comments section below:
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 - fallocatefallocate --length 8GiB swapfile
is suggested to create a swap file on Ubuntu man pages, please read Ubuntu Manpage - mkswapAfter all, if the created file contained holes in it,
sudo swapon
will throw an errorskipping - it appears to have holes.
and in this rare case which I haven't faced or heard of yet, the solution is simple. Just usedd
to create the file in step # 1 above and move on. Do it as follows:or as @v_mil did it:
In short, this concern is not worrying enough to give up on the speed gained by using
fallocate
.