Zen Tab Suspender

Tackling Zen Browser's Memory Issues

Arc is shutting down this December. The Browser Company announced the sunset and pointed users at Dia, its AI-first browser, which is built around a different idea of what a browser is for. So I moved to Zen Browser. Zen is good. It is also, on my machine, a lag problem.

Zen ships a tab unloader that never runs. Even when it does run, it only reacts to memory pressure. With a lot of tabs open, or memory strain on the system, Zen gets really laggy and becomes unusable, in a way that other browsers seem to handle.

A preference disables the unloader outright, and that is a one-line fix in about:config. The unloader also has no idle-time trigger, which needed an extension: github.com/danieljohnmorris/zen-tab-suspender.

The unloader that never fires

Zen ships these defaults:

pref("browser.tabs.unloadOnLowMemory", true);
pref("browser.lowMemoryResponseMask", 0);   // 0x0 = no response
pref("browser.lowMemoryResponseOnWarn", false);
pref("browser.tabs.min_inactive_duration_before_unload", 600000);

unloadOnLowMemory is on, which makes it look like the feature works. The mask decides whether the macOS memory watcher is allowed to call the unloader, and at 0 it never does.

Set the mask and the unloader starts working:

browser.lowMemoryResponseMask = 3
browser.lowMemoryResponseOnWarn = true

A tab then has to be idle for ten minutes, the min_inactive_duration_before_unload value, before it is eligible.

Pressure is not a schedule

Firefox’s tab unloader has no timer. It only runs when macOS reports warn or critical memory pressure, so until then idle tabs stay loaded however long they have been idle.

Zen used to have a setting of its own, zen.tab-unloader.enabled, with a timeout in minutes. It is gone from the current source tree.

How other browsers handle it

Firefox has unloaded tabs under memory pressure since 2021. Chrome went further in late 2022 with Memory Saver, which pauses tabs you have not touched in a while and keeps a list of sites that should never be paused. It later gained three levels: Moderate, Balanced, and Maximum.

Before that, the job fell to extensions. The popular one was The Great Suspender, with more than two million installs. It was sold to a new maintainer in 2020, and the next version shipped obfuscated code that pulled a script from a third-party server. Google removed it for containing malware and disabled it for everyone who had it installed.

What Zen Tab Suspender does

Zen Tab Suspender does for Zen a combination of what Chrome’s Memory Saver and the tab unloader extensions do. It takes the idle levels and the protections from Memory Saver, and the timer from the extensions.

Every 60 seconds it looks for tabs that have been idle past the threshold and calls browser.tabs.discard() on them. The threshold is 30 minutes by default, with levels at 10 and 120. Discarded tabs stay in the tab strip and reload when you select them.

Knowing when to leave a tab alone

Discarding a tab is one API call. The rest of the extension decides when to hold back. A tab playing audio, holding a half-filled form, or sharing a screen should survive the timer.

Firefox tells an extension whether a tab is active, pinned, or audible. It does not say whether a page is sharing a screen or talking to a USB device. Those APIs live in the page’s own JavaScript context, which an extension’s content script cannot see.

So the content script injects a second script into the page. It fetches page-script.js from the extension and hands it over as a blob URL:

const response = await fetch(api.runtime.getURL("page-script.js"));
const code = await response.text();
const blob = new Blob([code], { type: "text/javascript" });
const url = URL.createObjectURL(blob);
const script = document.createElement("script");
script.src = url;
script.dataset.nonce = nonce;

That script wraps the screen sharing, notification, USB, and Bluetooth APIs, and reports back when a page uses one. The nonce is random per frame, so the page cannot forge those reports.

If the injection fails, the extension cannot tell whether the tab is mid-call, so it leaves the tab loaded. Discarding a tab that was sharing its screen is worse than keeping one that was idle.

The iframe that never lets go

State is tracked per frame, because any iframe in a tab can be the one playing audio. Frames disappear without telling the extension, and an earlier version would keep a tab protected forever on the word of an iframe that no longer existed.

Each run now compares the stored frames against webNavigation.getAllFrames and drops the ones that are gone.

Testing it

I loaded the extension as a temporary add-on in a throwaway Zen profile. A tab with a half-filled form, a tab sharing its screen, and a tab playing audio all survived the timer. Idle background tabs were discarded and reloaded when selected.

Notifications, USB and Bluetooth devices, and the download pause are covered by unit tests against a mocked browser API.

Try it

The extension is at github.com/danieljohnmorris/zen-tab-suspender. To load it, open about:debugging#/runtime/this-firefox in Zen, click Load Temporary Add-on, and select manifest.json.

The two settings to change in your Zen are in about:config:

browser.lowMemoryResponseMask = 3
browser.lowMemoryResponseOnWarn = true