Closed Bug 784040 Opened 12 years ago Closed 12 years ago

[Native Fennec] Expire background tabs on low memory

Categories

(Firefox for Android Graveyard :: General, defect)

16 Branch
ARM
Android
defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED
Firefox 18

People

(Reporter: mfinkle, Assigned: kats)

References

Details

Attachments

(4 files)

XUL Fennec used a MemoryObserver class to perform GC's and drop background tabs in low memory situations. We also used a preference to disable the normal GC so we don't do it twice.

http://mxr.mozilla.org/mozilla-central/search?string=MemoryObserver
http://mxr.mozilla.org/mozilla-central/source/mobile/android/app/mobile.js#388

In Native Fennec we do not yet have the MemoryObserver code, but we still disable the normal GC (that is bad). We need to port the code or drop the preference.
Sidenote: this is perhaps a good place to put a hook for low memory database pruning, too.
OS: Linux → Android
Hardware: x86_64 → ARM
Note to future self: mfinkle suggested moving the dump code from bug 783230 into the MemoryObserver class once it is resurrected. That seems like a good idea.
I moved over MemoryObserver but took out the tab.resurrect() calls for now since I wasn't sure if they worked in native fennec. I'll check to see if that still works as expected and if so add a second patch that hooks that up to handleLowMemory().
Assignee: nobody → bugmail.mozilla
Attachment #665081 - Flags: review?(mark.finkle)
Attachment #665081 - Attachment description: Port MemoryObserver to native fennec → (1/4) Port MemoryObserver to native fennec
This one maybe doesn't need to go in, but I found it pretty useful.
Attachment #665145 - Flags: review?(mark.finkle)
Seems to work pretty well but might need a little more testing
Attachment #665146 - Flags: review?(mark.finkle)
Comment on attachment 665081 [details] [diff] [review]
(1/4) Port MemoryObserver to native fennec

>diff --git a/mobile/android/chrome/content/browser.js b/mobile/android/chrome/content/browser.js
>+var MemoryObserver = {

>+  observe: function mo_observe(aSubject, aTopic, aData) {
>+    if (aTopic == "memory-pressure") {
>+      if (aData != "heap-minimize") {
>+        this.handleLowMemory();
>+      }
>+      // The JS engine would normally GC on this notification, but since we
>+      // disabled that in favor of this method (bug 669346), we should gc here.
>+      // See bug 784040 for when this code was ported from XUL to native Fennec.
>+      this.gc();

Holy crap. I realize the the pref "javascript.options.gc_on_memory_pressure" is still false in mobile.js which means we have not been GC on memory pressure this whole time.

>+  dumpMemoryStats: function(aLabel) {

nit: We use "let" over "var". Could you switch?
Attachment #665081 - Flags: review?(mark.finkle) → review+
Attachment #665144 - Flags: review?(mark.finkle) → review+
Attachment #665145 - Flags: review?(mark.finkle) → review+
Comment on attachment 665146 [details] [diff] [review]
(4/4) Zombify background tabs on low memory

>diff --git a/mobile/android/chrome/content/browser.js b/mobile/android/chrome/content/browser.js

>+    // Make sure the previously selected panel remains selected. The selected panel of a deck is
>+    // not stable when panels are added.
>+    let selectedPanel = BrowserApp.deck.selectedPanel;
>+    BrowserApp.deck.insertBefore(this.browser, aParams.browserSibling || null);

nit: Let's just call "browserSibling" -> "sibling"

>   handleLowMemory: function() {

>+    var tabs = BrowserApp.tabs;
>+    var selected = BrowserApp.selectedTab;
>+    for (var i = 0; i < tabs.length; i++) {

"var" -> "let"


Nice patch. Looks like you have everything covered for resurrection.
Attachment #665146 - Flags: review?(mark.finkle) → review+
(In reply to Mark Finkle (:mfinkle) from comment #8)
> Holy crap. I realize the the pref "javascript.options.gc_on_memory_pressure"
> is still false in mobile.js which means we have not been GC on memory
> pressure this whole time.

Heh, yeah. I thought that's what you were referring to in comment 0 when you said "that is bad".

> >+  dumpMemoryStats: function(aLabel) {
> 
> nit: We use "let" over "var". Could you switch?

I can, although I'm curious as to why we do that. "let" is only supported by our JS engine and as far as i can tell identical to "var". Why not use the standard "var"?

Also I tested these patches some more and they seem to be holding up. Specifically I did this:

1) load http://people.mozilla.org/~kgupta/tmp/oom.html (it just keeps allocating memory)
2) stop oom after it has allocated about 10000 divs
3) open a new tab with about:blank
4) dump memory stats using adb shell am -a org.mozilla.gecko.MEMORY_DUMP
5) force zombification of the oom tab using adb shell -a org.mozilla.gecko.FORCE_MEMORY_PRESSURE
6) dump memory stats again
7) compare memory stats from (4) and (6) to ensure that we freed a bunch of memory. this was in fact the case, so i conclude this code works as expected.
CC'ing Brian because he was interested in working on some "stub tab" placeholders in the Java side and this might have some overlap with that work.
(In reply to Kartikaya Gupta (:kats) (away sep28-oct08) from comment #10)

> > nit: We use "let" over "var". Could you switch?
> 
> I can, although I'm curious as to why we do that. "let" is only supported by
> our JS engine and as far as i can tell identical to "var". Why not use the
> standard "var"?

"var" hoists the scope of the variable to function-level. "let" allows the scope to stay at the if/for block level. "let" allows the scope to be more like C or Java.
(In reply to Kartikaya Gupta (:kats) (away sep28-oct08) from comment #10)
> (In reply to Mark Finkle (:mfinkle) from comment #8)
> > Holy crap. I realize the the pref "javascript.options.gc_on_memory_pressure"
> > is still false in mobile.js which means we have not been GC on memory
> > pressure this whole time.
> 
> Heh, yeah. I thought that's what you were referring to in comment 0 when you
> said "that is bad".

I guess I did realize it, but just forgot about that. I'm glad this is getting fixed :)
Summary: Port MemoryObserver to Native Fennec → [Native Fennec] Expire background tabs on low memory
Blocks: 816381
Product: Firefox for Android → Firefox for Android Graveyard
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: