michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: var MemoryObserver = { michael@0: observe: function mo_observe(aSubject, aTopic, aData) { michael@0: if (aTopic == "memory-pressure") { michael@0: if (aData != "heap-minimize") { michael@0: this.handleLowMemory(); michael@0: } michael@0: // The JS engine would normally GC on this notification, but since we michael@0: // disabled that in favor of this method (bug 669346), we should gc here. michael@0: // See bug 784040 for when this code was ported from XUL to native Fennec. michael@0: this.gc(); michael@0: } else if (aTopic == "Memory:Dump") { michael@0: this.dumpMemoryStats(aData); michael@0: } michael@0: }, michael@0: michael@0: handleLowMemory: function() { michael@0: // do things to reduce memory usage here michael@0: let tabs = BrowserApp.tabs; michael@0: let selected = BrowserApp.selectedTab; michael@0: for (let i = 0; i < tabs.length; i++) { michael@0: if (tabs[i] != selected) { michael@0: this.zombify(tabs[i]); michael@0: Telemetry.addData("FENNEC_TAB_ZOMBIFIED", (Date.now() - tabs[i].lastTouchedAt) / 1000); michael@0: } michael@0: } michael@0: Telemetry.addData("FENNEC_LOWMEM_TAB_COUNT", tabs.length); michael@0: }, michael@0: michael@0: zombify: function(tab) { michael@0: let browser = tab.browser; michael@0: let data = browser.__SS_data; michael@0: let extra = browser.__SS_extdata; michael@0: michael@0: // We need this data to correctly create and position the new browser michael@0: // If this browser is already a zombie, fallback to the session data michael@0: let currentURL = browser.__SS_restore ? data.entries[0].url : browser.currentURI.spec; michael@0: let sibling = browser.nextSibling; michael@0: let isPrivate = PrivateBrowsingUtils.isWindowPrivate(browser.contentWindow); michael@0: michael@0: tab.destroy(); michael@0: tab.create(currentURL, { sibling: sibling, zombifying: true, delayLoad: true, isPrivate: isPrivate }); michael@0: michael@0: // Reattach session store data and flag this browser so it is restored on select michael@0: browser = tab.browser; michael@0: browser.__SS_data = data; michael@0: browser.__SS_extdata = extra; michael@0: browser.__SS_restore = true; michael@0: browser.setAttribute("pending", "true"); michael@0: }, michael@0: michael@0: gc: function() { michael@0: window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils).garbageCollect(); michael@0: Cu.forceGC(); michael@0: }, michael@0: michael@0: dumpMemoryStats: function(aLabel) { michael@0: let memDumper = Cc["@mozilla.org/memory-info-dumper;1"].getService(Ci.nsIMemoryInfoDumper); michael@0: memDumper.dumpMemoryInfoToTempDir(aLabel, /* minimize = */ false); michael@0: }, michael@0: };