From 2cb68401e0d71e7fc521c9928eafca52136f729c Mon Sep 17 00:00:00 2001 From: Cristian Tuns Date: Thu, 20 Jan 2022 04:03:20 -0500 Subject: [PATCH] Backed out changeset 62ff4a66c1c6 (bug 1750878) for causing Btime failures CLOSED TREE --- .../process/GeckoServiceChildProcess.java | 37 ++++++++-- .../gecko/process/MemoryController.java | 70 ------------------- .../org/mozilla/geckoview/GeckoRuntime.java | 5 -- .../geckoview_example/GeckoViewActivity.java | 1 - 4 files changed, 30 insertions(+), 83 deletions(-) delete mode 100644 mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/MemoryController.java diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/GeckoServiceChildProcess.java b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/GeckoServiceChildProcess.java index 5dfe82929193..5f8e7a08fae8 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/GeckoServiceChildProcess.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/GeckoServiceChildProcess.java @@ -6,6 +6,7 @@ package org.mozilla.gecko.process; import android.app.Service; +import android.content.ComponentCallbacks2; import android.content.Intent; import android.os.Binder; import android.os.Bundle; @@ -26,11 +27,13 @@ import org.mozilla.gecko.util.ThreadUtils; public class GeckoServiceChildProcess extends Service { private static final String LOGTAG = "ServiceChildProcess"; + // Allowed elapsed time between full GCs while under constant memory pressure + private static final long LOW_MEMORY_ONGOING_RESET_TIME_MS = 10000; private static IProcessManager sProcessManager; private static String sOwnerProcessId; - private final MemoryController mMemoryController = new MemoryController(); + private long mLastLowMemoryNotificationTime = 0; // Makes sure we don't reuse this process private static boolean sCreateCalled; @@ -191,16 +194,36 @@ public class GeckoServiceChildProcess extends Service { @Override public void onTrimMemory(final int level) { - mMemoryController.onTrimMemory(level); + Log.i(LOGTAG, "onTrimMemory(" + level + ")"); // This is currently a no-op in Service, but let's future-proof. super.onTrimMemory(level); - } - @Override - public void onLowMemory() { - mMemoryController.onLowMemory(); - super.onLowMemory(); + if (level < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) { + // We're not currently interested in trim events for non-backgrounded processes. + return; + } + + // See nsIMemory.idl for descriptions of the various arguments to the "memory-pressure" + // observer. + String observerArg = null; + + final long currentNotificationTime = System.currentTimeMillis(); + if (level >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE + || (currentNotificationTime - mLastLowMemoryNotificationTime) + >= LOW_MEMORY_ONGOING_RESET_TIME_MS) { + // We do a full "low-memory" notification for both new and last-ditch onTrimMemory requests. + observerArg = "low-memory"; + mLastLowMemoryNotificationTime = currentNotificationTime; + } else { + // If it has been less than ten seconds since the last time we sent a "low-memory" + // notification, we send a "low-memory-ongoing" notification instead. + // This prevents Gecko from re-doing full GC's repeatedly over and over in succession, + // as they are expensive and quickly result in diminishing returns. + observerArg = "low-memory-ongoing"; + } + + GeckoAppShell.notifyObservers("memory-pressure", observerArg); } /** diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/MemoryController.java b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/MemoryController.java deleted file mode 100644 index 38119cd94811..000000000000 --- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/MemoryController.java +++ /dev/null @@ -1,70 +0,0 @@ -package org.mozilla.gecko.process; - -import android.content.ComponentCallbacks2; -import android.content.res.Configuration; -import android.util.Log; -import androidx.annotation.NonNull; -import org.mozilla.gecko.GeckoAppShell; - -public class MemoryController implements ComponentCallbacks2 { - private static final String LOGTAG = "MemoryController"; - private long mLastLowMemoryNotificationTime = 0; - - // Allowed elapsed time between full GCs while under constant memory pressure - private static final long LOW_MEMORY_ONGOING_RESET_TIME_MS = 10000; - - private static final int LOW = 0; - private static final int MODERATE = 1; - private static final int CRITICAL = 2; - - private int memoryLevelFromTrim(final int level) { - if (level >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE - || level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL) { - return CRITICAL; - } else if (level >= ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) { - return MODERATE; - } - return LOW; - } - - public void onTrimMemory(final int level) { - Log.i(LOGTAG, "onTrimMemory(" + level + ")"); - onMemoryNotification(memoryLevelFromTrim(level)); - } - - @Override - public void onConfigurationChanged(final @NonNull Configuration newConfig) {} - - public void onLowMemory() { - Log.i(LOGTAG, "onLowMemory"); - onMemoryNotification(CRITICAL); - } - - private void onMemoryNotification(final int level) { - if (level == LOW) { - // The trim level is too low to be actionable - return; - } - - // See nsIMemory.idl for descriptions of the various arguments to the "memory-pressure" - // observer. - final String observerArg; - - final long currentNotificationTime = System.currentTimeMillis(); - if (level == CRITICAL - || (currentNotificationTime - mLastLowMemoryNotificationTime) - >= LOW_MEMORY_ONGOING_RESET_TIME_MS) { - // We do a full "low-memory" notification for both new and last-ditch onTrimMemory requests. - observerArg = "low-memory"; - mLastLowMemoryNotificationTime = currentNotificationTime; - } else { - // If it has been less than ten seconds since the last time we sent a "low-memory" - // notification, we send a "low-memory-ongoing" notification instead. - // This prevents Gecko from re-doing full GC's repeatedly over and over in succession, - // as they are expensive and quickly result in diminishing returns. - observerArg = "low-memory-ongoing"; - } - - GeckoAppShell.notifyObservers("memory-pressure", observerArg); - } -} diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java index 1ea0b102b5e9..83a6d8b7ee74 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java @@ -47,7 +47,6 @@ import org.mozilla.gecko.GeckoScreenOrientation.ScreenOrientation; import org.mozilla.gecko.GeckoSystemStateListener; import org.mozilla.gecko.GeckoThread; import org.mozilla.gecko.annotation.WrapForJNI; -import org.mozilla.gecko.process.MemoryController; import org.mozilla.gecko.util.BundleEventListener; import org.mozilla.gecko.util.DebugConfig; import org.mozilla.gecko.util.EventCallback; @@ -136,8 +135,6 @@ public final class GeckoRuntime implements Parcelable { */ public static final String CRASHED_PROCESS_TYPE_BACKGROUND_CHILD = "BACKGROUND_CHILD"; - private final MemoryController mMemoryController = new MemoryController(); - @Retention(RetentionPolicy.SOURCE) @StringDef( value = { @@ -555,8 +552,6 @@ public final class GeckoRuntime implements Parcelable { throw new IllegalStateException("Failed to initialize GeckoRuntime"); } - context.registerComponentCallbacks(runtime.mMemoryController); - return runtime; } diff --git a/mobile/android/geckoview_example/src/main/java/org/mozilla/geckoview_example/GeckoViewActivity.java b/mobile/android/geckoview_example/src/main/java/org/mozilla/geckoview_example/GeckoViewActivity.java index 3777ca10233e..67940cbffffb 100644 --- a/mobile/android/geckoview_example/src/main/java/org/mozilla/geckoview_example/GeckoViewActivity.java +++ b/mobile/android/geckoview_example/src/main/java/org/mozilla/geckoview_example/GeckoViewActivity.java @@ -1387,7 +1387,6 @@ public class GeckoViewActivity extends AppCompatActivity private void loadFromIntent(final Intent intent) { final Uri uri = intent.getData(); if (uri != null) { - createNewTab(); mTabSessionManager .getCurrentSession() .load(