Stop Linux from freezing

Story

Since I first switched to Linux, my computer always freezes every time I do heavy work. At the beginning, it froze a few times a month. When the laptop froze, I have to turn it off, no different from when I used Windows. Then its became more and more frequent, to the point that my computer froze almost daily. After researching, I found that my computer seems to freeze every time the RAM is nearly or completely full. My laptop have 2GB of RAM, I tried adding a 4GB swap partition, but it doesn't seem help. "Maybe it's because my laptop is so old" I thought to myself.

I got a better laptop when I go to college. With 8GB of RAM, the condition no longer occurs daily, but its frequency is still every week... Quite annoying, I can't open a lot of tabs on the web browser because I fear that will cause freezing. When I want to play 3D heavy game, I have to turn off every thing else. Et cetera...

I saved up money from competitions prizes, and finally bought a laptop myself! The device still has 8GB of ram, but the CPU and GPU are much more powerful. The condition was improved... It freezes about 2-3 time a month. There are times when I often play heavy games, and it quickly freezes every week...

Things really got worse when I switched to BlendOS, it freezes almost every day, for the first week I installed Blend. I quickly checked the system monitor, and realized that: For some reason BlendOS doesn't have swap!

Solution

I decided to add a swap, this time I gave it 16GB just to be sure:

btrfs filesystem mkswapfile --size 16G ~/.cache/swapfile
sudo swapon ~/.cache/swapfile

Then add this to /etc/fstab:

/var/home/USERNAME/.cache/swapfile	swap    swap    defaults 0 0

Since then, my PC never freezes ever again, like a dream come true! Now I can open more than 20 browser tabs with game and heavy application, all at the same time. In the past, I had to use my laptop carefully because I was afraid it would freeze, Now I can freely open as many applications as I want!

The lesson here is: If Linux often freezes due to full RAM, try increase swap, lot of swap! It seems like I almost solved the problem a while ago when I tried increasing the swap a bit, If I tried to increase it more, I might not have to upgrade my laptop~