swap file full while memory is half empty

htop shows 1gb/2gb used memory and 512mb/512mb used swap.

cat /proc/sys/vm/swappiness shows 10 and cat /proc/swaps shows

Filename Type Size Used

/dev/null partition 524288 524288

grep VmSwap /proc/*/status | sort -n -r --key=2.1 | head -5 shows

/proc/25760/status:VmSwap:        333976 kB
/proc/2769/status:VmSwap:         149664 kB
/proc/798/status:VmSwap:           17852 kB
/proc/800/status:VmSwap:           16460 kB
/proc/3725/status:VmSwap:           9648 kB

What is taking so much swap? swappiness 10 means to use swap only as close as possible to really need it (low memory) so how come this is not happening? Is it recommended to create a larger swap?

Thanks

edit: free -m (after the swap suddenly freed itself, not sure why and how)

total        used        free      shared  buff/cache   available
Mem:           2048        1311         184           3         552         733
Swap:           512         220         291

You have a small amount of RAM, and rather small swap.

That means that if you have a lot of programs open, or even a single browser with 5-6 tabs it will fill up the RAM. This leads to pressure to swap out stuff that is not in active use.

To make more sense of it, you should check which programs are hiding behind the PIDs you list. This can be checked with ps aux -q pidno which will additionally show you total memory useage by that process, user, and so forth.

When things are swapped out, there's no reason to read it in again before it's needed. Some software that is nominally running on your computer, may in reality be totally unused. Swapping such software makes sense.

On my laptop, right now, I have 8GiB of RAM, 4GiB available, and 1.5GiB of swap in use. Mysql is one of the things that is swapped out for me, which makes sense; I use it for development once a week, not every day.

In your case, you have a low amount of memory, and a low amount of swap. I would increase swap to the same amount as memory, at least, in your situation. This allows for better memory management by the kernel.

Some software, such as browsers, require large amounts of RAM. Chrome, with ten Ask Ubuntu tabs, and facebook open consumes close to 3GiB on my laptop.

To add more swap, you can simply use the following commands:

sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
sudo chmod 600 /swapfile
sudo mkswap /swapfile
echo "/swapfile    none    swap    sw    0   0" | sudo tee -a /etc/fstab
sudo swapon -a

swapon -s should now show your new swapfile. bs=1M count=2048 will create a 2048MiB file. Modify to suit your needs.