|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 "use strict"; |
|
5 |
|
6 var MemoryObserver = { |
|
7 observe: function mo_observe(aSubject, aTopic, aData) { |
|
8 if (aTopic == "memory-pressure") { |
|
9 if (aData != "heap-minimize") { |
|
10 this.handleLowMemory(); |
|
11 } |
|
12 // The JS engine would normally GC on this notification, but since we |
|
13 // disabled that in favor of this method (bug 669346), we should gc here. |
|
14 // See bug 784040 for when this code was ported from XUL to native Fennec. |
|
15 this.gc(); |
|
16 } else if (aTopic == "Memory:Dump") { |
|
17 this.dumpMemoryStats(aData); |
|
18 } |
|
19 }, |
|
20 |
|
21 handleLowMemory: function() { |
|
22 // do things to reduce memory usage here |
|
23 let tabs = BrowserApp.tabs; |
|
24 let selected = BrowserApp.selectedTab; |
|
25 for (let i = 0; i < tabs.length; i++) { |
|
26 if (tabs[i] != selected) { |
|
27 this.zombify(tabs[i]); |
|
28 Telemetry.addData("FENNEC_TAB_ZOMBIFIED", (Date.now() - tabs[i].lastTouchedAt) / 1000); |
|
29 } |
|
30 } |
|
31 Telemetry.addData("FENNEC_LOWMEM_TAB_COUNT", tabs.length); |
|
32 }, |
|
33 |
|
34 zombify: function(tab) { |
|
35 let browser = tab.browser; |
|
36 let data = browser.__SS_data; |
|
37 let extra = browser.__SS_extdata; |
|
38 |
|
39 // We need this data to correctly create and position the new browser |
|
40 // If this browser is already a zombie, fallback to the session data |
|
41 let currentURL = browser.__SS_restore ? data.entries[0].url : browser.currentURI.spec; |
|
42 let sibling = browser.nextSibling; |
|
43 let isPrivate = PrivateBrowsingUtils.isWindowPrivate(browser.contentWindow); |
|
44 |
|
45 tab.destroy(); |
|
46 tab.create(currentURL, { sibling: sibling, zombifying: true, delayLoad: true, isPrivate: isPrivate }); |
|
47 |
|
48 // Reattach session store data and flag this browser so it is restored on select |
|
49 browser = tab.browser; |
|
50 browser.__SS_data = data; |
|
51 browser.__SS_extdata = extra; |
|
52 browser.__SS_restore = true; |
|
53 browser.setAttribute("pending", "true"); |
|
54 }, |
|
55 |
|
56 gc: function() { |
|
57 window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils).garbageCollect(); |
|
58 Cu.forceGC(); |
|
59 }, |
|
60 |
|
61 dumpMemoryStats: function(aLabel) { |
|
62 let memDumper = Cc["@mozilla.org/memory-info-dumper;1"].getService(Ci.nsIMemoryInfoDumper); |
|
63 memDumper.dumpMemoryInfoToTempDir(aLabel, /* minimize = */ false); |
|
64 }, |
|
65 }; |