1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/chrome/content/MemoryObserver.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,65 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 +"use strict"; 1.8 + 1.9 +var MemoryObserver = { 1.10 + observe: function mo_observe(aSubject, aTopic, aData) { 1.11 + if (aTopic == "memory-pressure") { 1.12 + if (aData != "heap-minimize") { 1.13 + this.handleLowMemory(); 1.14 + } 1.15 + // The JS engine would normally GC on this notification, but since we 1.16 + // disabled that in favor of this method (bug 669346), we should gc here. 1.17 + // See bug 784040 for when this code was ported from XUL to native Fennec. 1.18 + this.gc(); 1.19 + } else if (aTopic == "Memory:Dump") { 1.20 + this.dumpMemoryStats(aData); 1.21 + } 1.22 + }, 1.23 + 1.24 + handleLowMemory: function() { 1.25 + // do things to reduce memory usage here 1.26 + let tabs = BrowserApp.tabs; 1.27 + let selected = BrowserApp.selectedTab; 1.28 + for (let i = 0; i < tabs.length; i++) { 1.29 + if (tabs[i] != selected) { 1.30 + this.zombify(tabs[i]); 1.31 + Telemetry.addData("FENNEC_TAB_ZOMBIFIED", (Date.now() - tabs[i].lastTouchedAt) / 1000); 1.32 + } 1.33 + } 1.34 + Telemetry.addData("FENNEC_LOWMEM_TAB_COUNT", tabs.length); 1.35 + }, 1.36 + 1.37 + zombify: function(tab) { 1.38 + let browser = tab.browser; 1.39 + let data = browser.__SS_data; 1.40 + let extra = browser.__SS_extdata; 1.41 + 1.42 + // We need this data to correctly create and position the new browser 1.43 + // If this browser is already a zombie, fallback to the session data 1.44 + let currentURL = browser.__SS_restore ? data.entries[0].url : browser.currentURI.spec; 1.45 + let sibling = browser.nextSibling; 1.46 + let isPrivate = PrivateBrowsingUtils.isWindowPrivate(browser.contentWindow); 1.47 + 1.48 + tab.destroy(); 1.49 + tab.create(currentURL, { sibling: sibling, zombifying: true, delayLoad: true, isPrivate: isPrivate }); 1.50 + 1.51 + // Reattach session store data and flag this browser so it is restored on select 1.52 + browser = tab.browser; 1.53 + browser.__SS_data = data; 1.54 + browser.__SS_extdata = extra; 1.55 + browser.__SS_restore = true; 1.56 + browser.setAttribute("pending", "true"); 1.57 + }, 1.58 + 1.59 + gc: function() { 1.60 + window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils).garbageCollect(); 1.61 + Cu.forceGC(); 1.62 + }, 1.63 + 1.64 + dumpMemoryStats: function(aLabel) { 1.65 + let memDumper = Cc["@mozilla.org/memory-info-dumper;1"].getService(Ci.nsIMemoryInfoDumper); 1.66 + memDumper.dumpMemoryInfoToTempDir(aLabel, /* minimize = */ false); 1.67 + }, 1.68 +};