From 48bb3648c551e0c95f3f322e11e5ea1be3290153 Mon Sep 17 00:00:00 2001 From: Andy Street Date: Mon, 21 Nov 2016 09:11:31 -0800 Subject: [PATCH] Drop CSSNode pool on low memory when app is backgrounded Summary: Depends on D4189532 Drops the CSSNodePool when memory gets low. Reviewed By: emilsjolander Differential Revision: D4190264 fbshipit-source-id: 94cd36d877372e0d6ebdd989661af74bde41486d --- .../react/uimanager/UIManagerModule.java | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java index 3edff08dd..3f6044aab 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java @@ -11,10 +11,13 @@ package com.facebook.react.uimanager; import javax.annotation.Nullable; -import java.util.List; import java.util.HashMap; +import java.util.List; import java.util.Map; +import android.content.ComponentCallbacks2; +import android.content.res.Configuration; + import com.facebook.common.logging.FLog; import com.facebook.react.animation.Animation; import com.facebook.react.bridge.Callback; @@ -78,6 +81,7 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements private final EventDispatcher mEventDispatcher; private final Map mModuleConstants; private final UIImplementation mUIImplementation; + private final MemoryTrimCallback mMemoryTrimCallback = new MemoryTrimCallback(); private int mNextRootViewTag = 1; private int mBatchId = 0; @@ -114,6 +118,11 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements return mModuleConstants; } + @Override + public void initialize() { + getReactApplicationContext().registerComponentCallbacks(mMemoryTrimCallback); + } + @Override public void onHostResume() { mUIImplementation.onHostResume(); @@ -133,6 +142,9 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements public void onCatalystInstanceDestroy() { super.onCatalystInstanceDestroy(); mEventDispatcher.onCatalystInstanceDestroyed(); + + getReactApplicationContext().unregisterComponentCallbacks(mMemoryTrimCallback); + CSSNodePool.get().clear(); } private static Map createConstants(List viewManagerList) { @@ -549,4 +561,25 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements public int resolveRootTagFromReactTag(int reactTag) { return mUIImplementation.resolveRootTagFromReactTag(reactTag); } + + /** + * Listener that drops the CSSNode pool on low memory when the app is backgrounded. + */ + private class MemoryTrimCallback implements ComponentCallbacks2 { + + @Override + public void onTrimMemory(int level) { + if (level >= TRIM_MEMORY_MODERATE) { + CSSNodePool.get().clear(); + } + } + + @Override + public void onConfigurationChanged(Configuration newConfig) { + } + + @Override + public void onLowMemory() { + } + } }