Optimizing Linux RAM for Gaming
Introduction
This post is particularly useful for those using computers with limited RAM <= 8GB and has no option to increase RAM size due to hardware restriction (soldered memory slot) (I'm one of them).
The audience might say: "Why don't you just build a proper gaming PC?". Well you should if you play games a lot, I play games sporadically and it's quite rare to see me game even once every two weeks.
So yeah, I might build a $2,000 gaming rig but then it's going to waste coz I would rarely use it..or maybe I would use it often but because I feel I've spend some money to build the rig, so I NEED to play games. No, I play games only if I want to, not because I have to.
OOM - Out Of Memory
One of the most common issues for PCs with limited amount of memory is...you've guessed it: out of memory! At this point I prefer the approach taken by Linux: the OS would just terminate the task/game causing OOM, instead of Windows where it keeps on trying to run the game even though it's unplayable due to OOM ~ in the end we would summon The Task Manager! (that is if the Task Manager still responding LOL!!)
Enjoy some of these hilarious task manager memes:
Traitor!
Confused!
Swapfile / Virtual Memory
Some say increasing swapfile is like a cheat to increase RAM...well it's both Yes and No. Yes you're getting a more memory in terms of capacity, but no you're not getting the speed equivalent of a real RAM stick. Swapfile / virtual memory is much slower compared to RAM, some numbers I got from quick searching about it: swapfile only managed to get around 15% the speed of RAM.
So what's the point if this "fake" RAM is that slow? If you're mostly playing fast paced games such as FPS or racing game, then I'm sorry brother you're fucked and out of luck!
However, if you play the genres I play, then the swapfile / virtual memory could be a differentiator between an absolute ass gaming session or an enjoyable one. These genres are:
- Turn based strategy/tactics : Age of Wonders / XCOM, Battletech.
- Realtime with pause RPG : Pillars of Eternity.
- Deck-building / Trading Card games : Mythgard, Witcher Thronebreaker, Gwent.
- Sport Management: Football Manager.
There's a clear pattern there: anything not requiring real time / fast pace. So actually there are quite a lot of games for us not so lucky with our RAM arrangement!
Increase the fake RAM
Can be done easily using gparted via a Linux USB installer, in my case the installer for MX-Linux. There's a good Youtube video showing how to do this.
Note: don't forget the part to turn on the swap! Also the rule of thumb is to put 1 - 1.5x of the amount of RAM you got. My RAM is 8 GB so I set the total swapfile to 12-13 GB.
For non-systemd
Since MX-Linux uses sysVinit, there are several extra steps needed:
Check the volume using the command:
lsblk
From there you'll get the volume number for your swap partition.Modify fstab:
sudo vim /etc/fstab
Add the following line:/dev/volume_number non swap sw 0 0
Modify swappiness:
sudo vim /etc/sysctl.conf
Add the following line:vm.swappiness=25
Swappiness will determine how often the swapfile gets used, the default for MX-Linux is 15, while default for Ubuntu is 60. A rule of thumb for swappiness: low number (below 10) if you got high RAM (you don't actually need swap), but if you got low RAM then set the swappiness to between 20-50 depending on your use case (if you set too low then out of memory and programs starting to crash).
Conclusion
There you go, a simple trick to make your gaming session smoother and more enjoyable by managing your swapfile / virtual memory.