2

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?

  • 1
    would you please add the output of sudo sysctl vm.swappiness to your question. – Raffa Aug 13 at 16:21
  • 1
    Thanks! The information has been added. – v_mil Aug 13 at 16:28
  • 1
    Please run sudo 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 askubuntu.com/a/103916/968501 – Raffa Aug 13 at 16:35
  • @Raffa no, vm.swappiness=10 is the wrong way to go with 4G RAM. vm.swappiness=80 would be a better place to start. – heynnema Aug 13 at 20:03
  • @heynnema I see what you mean. Higher numbers tell the system to swap more. This is true. But sometimes when higher numbers do not work, smaller numbers do. I have no explanation for it, but that is what I observed. Plus the swappiness is already set to 60 which is high enough and I doubt an even higher number will make a difference – Raffa Aug 13 at 20:31
2

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.

  • 1
    no, don't use fallocate to create /swapfile, use dd. Search for info about this here on AU. – heynnema Aug 13 at 20:06
  • 1
    @heynnema the dd method would be sudo dd if=/dev/zero of=/swapfile bs=1024 count=8388608 but why? This is much slower than fallocate plus it makes no difference when it comes to swap areas. – Raffa Aug 13 at 20:22
  • If you check the other questions/answers here on AU that I mention, you'd see there IS a difference. – heynnema Aug 13 at 20:44
  • 1
    Thanks! It works. I did not try fallocate because on SSD sudo dd if=/dev/zero of=/swapfile bs=1048576 count=8192 operates less than a minute. /etc/fstab has been edited imediatly after the steps above and checking the swap by sudo swapon --show Now I will reboot using LiveCD for enlarging the OS partition to utilize the space of the swap partition. – v_mil Aug 13 at 20:54
  • @v_mil You are most welcome. Using dd with a high number of bytes=1048576 is more or less the same as fallocate. Glad it helped. – Raffa Aug 13 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.