Permalink
Browse files

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
  • Loading branch information...
astreet authored and Facebook Github Bot committed Nov 21, 2016
1 parent bd8745b commit 48bb3648c551e0c95f3f322e11e5ea1be3290153
Showing with 34 additions and 1 deletion.
  1. +34 −1 ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java
@@ -11,10 +11,13 @@

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 @@
private final EventDispatcher mEventDispatcher;
private final Map<String, Object> mModuleConstants;
private final UIImplementation mUIImplementation;
private final MemoryTrimCallback mMemoryTrimCallback = new MemoryTrimCallback();

private int mNextRootViewTag = 1;
private int mBatchId = 0;
@@ -114,6 +118,11 @@ public String getName() {
return mModuleConstants;
}

@Override
public void initialize() {
getReactApplicationContext().registerComponentCallbacks(mMemoryTrimCallback);
}

@Override
public void onHostResume() {
mUIImplementation.onHostResume();
@@ -133,6 +142,9 @@ public void onHostDestroy() {
public void onCatalystInstanceDestroy() {
super.onCatalystInstanceDestroy();
mEventDispatcher.onCatalystInstanceDestroyed();

getReactApplicationContext().unregisterComponentCallbacks(mMemoryTrimCallback);
CSSNodePool.get().clear();
}

private static Map<String, Object> createConstants(List<ViewManager> viewManagerList) {
@@ -549,4 +561,25 @@ public void addUIBlock (UIBlock block) {
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() {
}
}
}

0 comments on commit 48bb364

Please sign in to comment.