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
This commit is contained in:
Andy Street 2016-11-21 09:11:31 -08:00 committed by Facebook Github Bot
parent bd8745b1fd
commit 48bb3648c5
1 changed files with 34 additions and 1 deletions

View File

@ -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<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 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<String, Object> createConstants(List<ViewManager> 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() {
}
}
}