No swapping on LUbuntu at enabled swap

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:

  1. Create the swap file ( 8 Gib ):

    sudo fallocate -l 8G /swapfile
    
  2. Give the file right permissions to limit unneeded access:

    sudo chmod 600 /swapfile
    
  3. Prepare the file as a swap area:

    sudo mkswap /swapfile
    
  4. Activate the swap file:

    sudo swapon /swapfile
    
  5. 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.