Why MGLRU is so special? First, we need to know that there is an algorithm for managing the page cache in the Linux kernel. It works by keeping track of which pages have been used most recently and which ones have not. When the system needs to free up memory, it selects the page that has been used the least recently and swaps it out to disk. This is called LRU algorithm, which stands for Least Recently Used algorithm, used in the memory management system.
However, as we know it, the traditional active/inactive LRU algorithm used in Linux kernel is not doing a very good job. The common issue is high CPU usage in OOM situations. Therefore, we need a better LRU algorithm, MGLRU? According to Google, tested on tens of millions of ChromeOS users and about a million Android users, MGLRU shows an overall 40% decrease in kswapd CPU usage, in addition to improvements in other UX metrics, e.g., an 85% decrease in the number of low-memory kills and an 18% decrease in rendering latency.
To put it simply, just by enabling the magical MGLRU, you can save yourself to some extent from the OOM kills today!
Note, you need to use Linux kernel 6.1 or above to enable this feature. As of this writing, openSUSE Tumbleweed runs on Linux kernel 6.2.8. It's an advantage of running on a rolling release, you don't have to wait for months or even years for some major improvements.
Linux kernel 6.3 will bring further improvements to MGLRU also. See the news on Phoronix.
Enable MGLRU
-
Check whether your kernel enable
CONFIG_LRU_GEN
. You'll need to know your current kernel name in GNOME about page in your system settings. Or rununame -r
in the terminal. Then, go to/boot
and copy your kernel config's file name. For example,config-6.2.8-1-default
which is my current kernel config file. Open the terminal and type:
cat /boot/config-6.2.8-1-default | grep CONFIG_LRU_GEN
If this returns `y`, your kernel supports `MGLRU`, you can proceed to the next step.
2. Open plain text editor, entering this service file code and save it as `mglru.service`:
```
[Unit]
Description=Multi-Gen LRU Enabler Service
ConditionPathExists=/sys/kernel/mm/lru_gen/enabled
[Service]
Type=oneshot
# Turn on MGLRU, valid values: [0; 7] and [yYnN]
ExecStart=/bin/bash -c "/bin/echo y > /sys/kernel/mm/lru_gen/enabled"
# Set the thrashing prevention vaule in milliseconds, valid values: >= 0
ExecStartPost=/bin/bash -c "/bin/echo 1000 > /sys/kernel/mm/lru_gen/min_ttl_ms"
[Install]
WantedBy=default.target
Move the
mglru.service
file to/etc/systemd/system
using GNOME Files in admin mode by usingnautilus admin:///etc/systemd/system
command.Open YaST Services Manager and enable the service to run on boot.
Reboot the system.
-
Check whether MGLRU is enabled on your system by:
cat /sys/kernel/mm/lru_gen/enabled
This should return `0x0007`. If it returns `0x0000`, this means it's not enabled yet.
```
cat /sys/kernel/mm/lru_gen/min_ttl_ms
This should return `1000` for the optimal thrashing prevention.
There's an open issue to enable MGLRU by default on openSUSE. If you're interesting, you can participate by submitting your test results. See comment #10 in the issue thread for more details.
Cover Photo by Resource Database on Unsplash
Abstract Photo by Resource Database on Unsplash
Top comments (5)
Just as an FYI I just tried this out on my Ubuntu 22.04 system (I disabled systemd-oomd quite a while ago since I didn't care to have it killing off VirtualBox for me; currently running 6.2.0-36-generic Ubuntu kernel.) I did not set a min_ttl_ms either since, again, I want good swap management, not a "relief value" killing processes as I load it up.
So... things with lru_gen run GREAT... up to a point. I'm running 8GB of VirtualBox VMs on a 16GB system to provide "a bit" of memory pressure. Everything ran great for a while; then the clock stopped, then top, the window manager became unresponsive (the copy of mpv I had playing a video kept running great though!) The browser became unusable pretty quickly. I even got some ~5-30 second full desktop freezes as I suppose Xorg got swapped out. Then eventually (without closing anything down, partially because my console became unresponsive).. it'd get everything it needed back in RAM and things would run fine again for a while. Under this memory load with conventional lru_gen off, you get a small amount of "jank" switching from one task to the next but nothing becomes unresponsive for long.
In other words, lru_gen ran noticeably better under light to moderate loads, then totally went to pieces under heavier load.
Is it neccesary to create service file? There is a line CONFIG_LRU_GEN_ENABLED commented in boot file and I read in bugzilla that is enough to enable service:
bugzilla.suse.com/show_bug.cgi?id=...
echo y >/sys/kernel/mm/lru_gen/enabled
Is this true?
At least, on openSUSE Tumbleweed, I can't seem to enable MGLRU by
echo y >/sys/kernel/mm/lru_gen/enabled
at runtime. Therefore, I use the service file.Ok this is epic.
Thanks!