![Finetuning kernel for high swap environment](https://linux22.com/image/183737/Finetuning%20kernel%20for%20high%20swap%20environment.png)
I own an 8GB RAM Dell XPS13 9343; the amount of RAM is the biggest pain of this computer, it's soldered on mainboard. Despite the CPU supporting more RAM, even resoldering bigger ones with firmware modification is not an option due to the mainboard design. So make this computer still useful, I replaced M.2 SATA disk with faster M.2 NVMe one and added two extra swapfiles.
My standard config for internet browsing is 8GM RAM + 8GM swap partition, while on-demand I have two extra swapfiles residing on ext4/(root). This gives me in total 32GB swap. It's also worth mentioning that all filesystems including all swaps reside on encrypted LUKS + LVM. This is intentional as I don't want my RAM to be exposed when the computer is turned off.
This setup generally does work; when I enable a lot of memory hungry (usually Java + browser based content, occasionally a single virtual machine), I just turn on both swapfiles and after all needed apps are launched the total load doesn't exceed 20% CPU and total disk I/O doesn't exceed 20M/s.
Now the question part: whole solution occasionally - once a week, when I use memory intensive task few times a week becomes unstable/unreliable. Sometimes it reboots - not sure if it's kernel panic of some hardware issue but graphic card buffers gets corrupted for less than a second then computer restarts. I'm unable to use kdump-tools to debug kernel panic as thanks to Dell's firmware my UEFI doesn't recognize the NVMe disk and I need to put rEFInd on USB only for the boot stage. The second issue is that occasionally when CPU loads increase and I play music in the background, it starts to stutter along with my X11.
While some parts are expected as this is not decent hardware I'm worried that this load can affect audio buffers and so I'm looking for a way to make this more stable. IMHO the load should affect only userspace apps without making those system-wide problems.
I'm using Ubuntu on it and I'm aware there exists a low-latency kernel but wondering if I can first test some lighter solution which can be switched back and forth at runtime. Like things related to amount of file descriptors or assigning some priorities via cgroups?
答案1
Swap will always be a very inefficient and there isn't really much you can do about that. There are two options you might try: zram and zswap. Both use RAM to make swap a little more usable.
Zram allows you to create a swap from your memory -- it creates a new block device that will be used as swap, but pages saved to it are compressed and you can achieve about 50 % compression rate (so 2 GiB swap on zram is roughly equivalent to 4 GiB swap on disk and much faster). You can have multiple swap devices, so you can have smaller swap in zram with higher priority and bigger swap on disk.
Zswap is similar to zram, but it is just a cache for the swap on disk. Again pages saved in zswap are compressed and written to disk only when zswap reaches its capacity.
答案2
If you need that much SWAP space you'd better buy a better PC.
SSD disks absolutely don't like being used as SWAP because it leads to their degradation faster.
Speaking of your issue: install and enable earlyoom
- that's your distro specific, so I can't say how you can do that.
答案3
A lighter weight distro is required, since you have insufficient RAM for your "heavyweight" applications.
答案4
Just for your information in my case this was caused by some bug in linux/ubuntu kernel, after upgrading to the newest one even if the system is ultimately loaded and slow it no longer crashes. https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1906107