Reposted from https://dev.to/archerallstars/enable-the-magical-multi-gen-lru-aka-mglru-on-opensuse-tumblewed-stay-away-from-oom-today-ji6

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

  1. 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 run uname -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 /etc/systemd/system/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
  3. Enable service using systemctl daemon-reload and systemctl enable mglru
  4. Reboot the system.
  5. 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.