is it a good idea to have an extension that forces GC and minimize memory usage on every tab close

General project discussion.
Use this as a last resort if your topic does not fit in any of the other boards but it still on-topic.
Forum rules
This General Discussion board is meant for topics that are still relevant to Pale Moon, web browsers, browser tech, UXP applications, and related, but don't have a more fitting board available.

Please stick to the relevance of this forum here, which focuses on everything around the Pale Moon project and its user community. "Random" subjects don't belong here, and should be posted in the Off-Topic board.
blue_dragon
Moonbather
Moonbather
Posts: 51
Joined: 2024-05-02, 09:21
Location: Earth

is it a good idea to have an extension that forces GC and minimize memory usage on every tab close

Post by blue_dragon » 2026-07-18, 10:54

i see Pale Moon currently dont free much memory when opening a new site on a tab and closing right away so i tried making this

Code: Select all

const Cc = Components.classes, Ci = Components.interfaces, Cu = Components.utils;
const gMgr = Cc['@mozilla.org/memory-reporter-manager;1'].getService(Ci.nsIMemoryReporterManager);
Cu.import("resource://gre/modules/Services.jsm");

function onTabClose() {
	// gc
	Services.obs.notifyObservers(null, 'child-gc-request', null);
	Cu.forceGC();

	// minimize memory usage
	Services.obs.notifyObservers(null, 'child-mmu-request', null);
	gMgr.minimizeMemoryUsage(() => {});
}

const windowListener = {
	onOpenWindow(xulWindow) {
		const domWindow = xulWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
		domWindow.addEventListener('load', function onLoad() {
			domWindow.removeEventListener('load', onLoad);
			if(domWindow.document.documentElement.getAttribute('windowtype') !== 'navigator:browser')
				return;
			domWindow.gBrowser.tabContainer.addEventListener('TabClose', onTabClose);
		});
	},
	onCloseWindow(xulWindow) {
		const domWindow = xulWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
		if(domWindow.document.documentElement.getAttribute('windowtype') !== 'navigator:browser')
			return;
		domWindow.gBrowser.tabContainer.removeEventListener('TabClose', onTabClose);
	},
};

function startup(data, reason) {
	Services.wm.addListener(windowListener);
}

function shutdown(data, reason) {
	Services.wm.removeListener(windowListener);
}

function install() {}

function uninstall() {}
it seems to be somehow effective but closing tab causes lag spikes. also im not sure if this is a good approach since i have seen some memory management extensions randomly crash the browser. but reducing memory usage is needed especially in 32-bit OS because if Pale Moon ever hits 1GB ram usage it becomes super unstable (tested on Windows 7 even on the official build)

i see that idle/tab close gc was once implemented but reverted due to performance issues recently although i didnt feel a big perf drop

User avatar
Moonchild
Project founder
Project founder
Posts: 39714
Joined: 2011-08-28, 17:27
Location: Sweden

Re: is it a good idea to have an extension that forces GC and minimize memory usage on every tab close

Post by Moonchild » 2026-07-18, 17:22

You have to choose one or the other.
Either let GC do its thing on its own and let memory be recovered, OR force GC and CC immediately on close, and have the browser pause while it does that.
"Sales hates anything that can't be turned into a confident sentence." - anonymous warehouse worker
"Why debate someone you fundamentally don't trust?" - Dario Amodei
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite