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
4

2 Answers

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.

4

Check out this Article Credit goes to this : Article Link

Webmin: How to change the size of Swap?

January 6, 2020Category:Linux AdministrationTags:linux, swap, virtualmin, webmin Even after searching on internet, I could not find a way to change the Swap size. Various online tutorials only showed us how to create swap space but did not explicitly address how to change the size of swap space using Webmin. Of course, this tutorial does not use Webmin but instead it uses commands against the server.

Prerequisites/Assumptions

Swap space already exists
/swapfile is the file created for swap space
I am changing the /swapfile from 270MB to 1GB

Commands to Resize Swap Space

sudo swapoff /swapfile
sudo fallocate -l 1G /swapfile
ls -lh /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
free

Command Explanation

Turns off/unmounts current swap
Resizes swap file to 1G (or your desired size)
Verifies the file size and permission. (Make sure permission is -rw——-)
Marks this file as swap file. Without this command, system will use previous size
Turns on swap file
check free space

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like