Skip to content

Commit 48bb364

Browse files
astreetFacebook Github Bot
authored andcommitted
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
1 parent bd8745b commit 48bb364

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111

1212
import javax.annotation.Nullable;
1313

14-
import java.util.List;
1514
import java.util.HashMap;
15+
import java.util.List;
1616
import java.util.Map;
1717

18+
import android.content.ComponentCallbacks2;
19+
import android.content.res.Configuration;
20+
1821
import com.facebook.common.logging.FLog;
1922
import com.facebook.react.animation.Animation;
2023
import com.facebook.react.bridge.Callback;
@@ -78,6 +81,7 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements
7881
private final EventDispatcher mEventDispatcher;
7982
private final Map<String, Object> mModuleConstants;
8083
private final UIImplementation mUIImplementation;
84+
private final MemoryTrimCallback mMemoryTrimCallback = new MemoryTrimCallback();
8185

8286
private int mNextRootViewTag = 1;
8387
private int mBatchId = 0;
@@ -114,6 +118,11 @@ public Map<String, Object> getConstants() {
114118
return mModuleConstants;
115119
}
116120

121+
@Override
122+
public void initialize() {
123+
getReactApplicationContext().registerComponentCallbacks(mMemoryTrimCallback);
124+
}
125+
117126
@Override
118127
public void onHostResume() {
119128
mUIImplementation.onHostResume();
@@ -133,6 +142,9 @@ public void onHostDestroy() {
133142
public void onCatalystInstanceDestroy() {
134143
super.onCatalystInstanceDestroy();
135144
mEventDispatcher.onCatalystInstanceDestroyed();
145+
146+
getReactApplicationContext().unregisterComponentCallbacks(mMemoryTrimCallback);
147+
CSSNodePool.get().clear();
136148
}
137149

138150
private static Map<String, Object> createConstants(List<ViewManager> viewManagerList) {
@@ -549,4 +561,25 @@ public void addUIBlock (UIBlock block) {
549561
public int resolveRootTagFromReactTag(int reactTag) {
550562
return mUIImplementation.resolveRootTagFromReactTag(reactTag);
551563
}
564+
565+
/**
566+
* Listener that drops the CSSNode pool on low memory when the app is backgrounded.
567+
*/
568+
private class MemoryTrimCallback implements ComponentCallbacks2 {
569+
570+
@Override
571+
public void onTrimMemory(int level) {
572+
if (level >= TRIM_MEMORY_MODERATE) {
573+
CSSNodePool.get().clear();
574+
}
575+
}
576+
577+
@Override
578+
public void onConfigurationChanged(Configuration newConfig) {
579+
}
580+
581+
@Override
582+
public void onLowMemory() {
583+
}
584+
}
552585
}

0 commit comments

Comments
 (0)