That's right. I did this and forgot about it, and was wondering why my laptop freezes after running a single IntelliJ, Chrome with 20 open tabs, docker desktop with 4 running containers and maybe a single nodejs process.
I thought it would be a good idea to minimize swap space to 2GB, since I have 16GB RAM.
I have a Dell XPS 15, it's now 4-5 years old, I can't remember. Other than changing thermal paste yearly, it really still runs like new. I'd probably buy it again when current one gets too old.
Back to the topic, and my misunderstanding of swap use.
What I did know is that RAM is used for running an app, and when kernel decides RAM is not enough, it switches to swap space, that is, writes to disk. And when swap space is used, you'll experience some slowness. Naturally, RAM is faster.
And because RAM is faster, I thought it would be good to put to use rest of free RAM instead of swap.
1st Misunderstanding: Free RAM
Of 16GB total RAM, if I have 10GB of RAM used, then I have 6GB of free RAM, right?
Wrong.
Linux uses the rest for cache.
More on it in this post https://askubuntu.com/a/159358/1041773. I won't copy the author further, just read the well written explanation.
2nd Misunderstanding: Decreasing swappiness increases performance
Swappiness is a system property valued from 0 to 100, and depending on that property your system decides it's time to turn the swap on.
If you set it to 0, swap space won't be used at all. But you risk system failures. What I mentioned earlier, RAM gets used up, or doesn't get fully used, but your system can't write to cache.
If you set it to 100, you get aggressive swapping.
I think 60 is default on Ubuntu. I changed that value to 10. I can't give you a good reason why, 60 seemed to be too aggressive with a lot of RAM available. Maybe you want to experiment with different values.
# Check your swappiness
cat /proc/sys/vm/swappiness
#
sudo sysctl vm.swappiness=10
How much swap space should I use
I decided to go with 1.5x RAM on my laptop, recommended is 2x RAM, and what/why is explained well in this post https://itsfoss.com/swap-size/
Set your swap space:
sudo dd if=/dev/zero of=/swapfile bs=1G count=24
# Set the correct permissions
sudo chmod 0600 /swapfile
sudo mkswap /swapfile # Set up a Linux swap area
sudo swapon /swapfile # Turn the swap on
Check if it worked with:
grep Swap /proc/meminfo
And make it permanent by editing /etc/fstab
/swapfile swap swap sw 0 0
All changes applied, and my system doesn't freeze.
The other question is why I got to 10GB that fast.
30 opened tabs don't help, but there are also some Chrome extensions that contribute to memory pile.
IntelliJ is just greedy, but you can go into plugins and disable the ones you don't use. About vm.options, I used to tinker with them in the past. Now I usually leave the default options that IntelliJ sets up for me. Indexing is the most intensive task, so be sure to exclude unimportant directories.
As for node processes, sometimes they're still left to run in the background although I exit the app.
Maybe it's the way I run the app, with hot reload. I didn't investigate further, I just occasionally run `killall node`,