I’m not sure systemd oom service being enabled by itself will do what you want. Looking at the output of oomctl dump
the oom memory pressure duration settings for what i think is my desktop session is like 20seconds. So the oom default settings might not actually be tuned to respond quickly enough. oom is complicated. To get the oom to respond faster, may require some tuning.
But that being said, here is how I would test to see if I could set a hard memory limit for firefox or other application running under my GNOME desktop.
First I’d identify the systemd unit name for my current firefox.
systemd-cgls | grep firefox | grep scope
This should show you the systemd scope associated with a firefox application started from either the gnome or kde destops i think.
For me its gnome, so I then run this to see the systemd configuration
systemctl --user cat app-gnome-org.mozilla.firefox-XXXX.scope
XXXX is a think the control group identifier or PID of the parent process in the control group. The point is its an unique tacked on to the end of the discoverable name.
There’s nothing in the configuration that suggests any hard memory limits. I can start trying to figure out what limits will work using systemd-run.
Using systemd-run from a console to play test hard memory limit settings.
systemd-run --quiet --user --scope -p MemoryMax=500M /usr/b-p MemorySwapMax=0 in/firefox
We these settins firefox crashes immediately.. that 500M with no swap is not enough.
So i tune those 2 a bit so I can get firefox up and running with a max on memory, but using some swap if it needs.
systemd-run --quiet --user --scope -p MemoryMax=4G /usr/b-p MemorySwapMax=1G in/firefox
I can then check the generated scope and see that the MemoryMax property is set
systemd --user cat run-XXXXX-YYYYY.scope
Cool, now I can edit the gnome session managed firefox and add the settings I want just as they appear in my test under systemctl-run
systemd --user edit app-gnome-org.mozilla.firefox-XXXX.scope
and add as directed:
[Scope]
MemoryMax=4G
MemorySwapMax=1G
That file gets saved and now if I do systemctl --user cat again I see the properties as active.
But if I want to make this work for all future firefox instances managed by gnome all i need to do is rename the directory holding the generated override file to app-gnome-org.mozilla.firefox-.scope.d
And when I logout and back into the desktop all firefox instances started by gnome will have those memory and swap limits applied!
Heres a good write up on what that looks like in the context of KDE.
Following that writeup, I’d just need to change the config filename to:
~/.config/systemd/user/app-gnome-org.mozilla.firefox@.scope.d/override.conf
And place the systemd scope properties I want to make sure are enabled into this file. systemd should read that file when my gnome desktop fires up an firefox application.
You could go even further and have everything listed under app-
with similar hard memory limits.