Run firefox avoiding system crashes - are there better ways?

If I run firefox unrestrected that crashes my system from time to time. I’ve been using the systemd-run command to run firefox whilst maintaining system stability like so:

systemd-run --scope -p MemoryLimit=20000M firefox &

I’d like to get some advice or suggestions of any better ways to do this and also the best way to automate it so that it happens even if I just click on the Firefox icon.

If I run Firefox in my default way then regularly, after some time, I run out of memory. This can cause multiple issues:

  • firefox locks up for a time and then recovers
  • The GUI locks up for some time but then recovers (recovery after over 1/2 hour has happened)
  • firefox locks up and crashes
  • X locks up for a long time and crashes, logging me out
  • the system as a whole locks up for too long (I’ve tested up to 10 hours)

If I run in a cgroup with systemd-run like above, then I still have the problem that Firefox occasionally crashes, but when it does so at least the system seems to survive and so I can just restart Firefox. The cause of the crash when cgroups is active seems to be that firefox calculates memory usage in a way that is not cgroups friendly, using meminfo

I’d like to know if there’s a better way to protect the system against Firefox excessive memory usage and, also, as I said before, how to ensure this happens automatically.

I have a firefox bug open which describes the problems when running without memory limits. Information that might help that get fixed also appreciated.

I’d like to get some advice or suggestions of any better ways to do this and also the best way to automate it so that it happens even if I just click on the Firefox icon.

You can always create a .desktop file for firefox with an Exec key that uses your wrapper.

1 Like

Thanks, it’s a good suggestion to get around the automation and I found some examples of configuring it in the configuration.nix so it can be pretty useful, but it doesn’t solve the issue that the oom-killer kills the wrong process in firefox.

One idea seems to be the OOMPolicy which sysetmd supports. You can set it on a normal service, but what I don’t understand is how to set the OOMPolicy when a user is running from systemd-run (if that’s possible).

systemd-run accepts arbitrary properties via -p, see the man pages for details. So anything you can set for a service you can use, including things like ManagedOOMSwap=kill. I have not spent that much time with oomd etc, but it seems like from man systems.resource-control that’s the first step to getting oomd to care specifically about a given cgroup.