Drop JSC code on background

Reviewed By: lexs

Differential Revision: D3311037

fbshipit-source-id: e46559108c51f1cd163ed5c557d23c21f696ef88
This commit is contained in:
Charles Dick
2016-05-26 11:07:41 -07:00
committed by Facebook Github Bot 8
parent bdab834036
commit f948662013
19 changed files with 102 additions and 1 deletions

View File

@@ -20,6 +20,7 @@ import static android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_MODERATE;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN;
/**
* Translates and routes memory pressure events to the current catalyst instance.
@@ -27,6 +28,8 @@ import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL;
public class MemoryPressureRouter {
// Trigger this by sending an intent to your activity with adb shell:
// am broadcast -a com.facebook.catalyst.ACTION_TRIM_MEMORY_MODERATE
private static final String ACTION_TRIM_MEMORY_UI_HIDDEN =
"com.facebook.rnfeed.ACTION_TRIM_MEMORY_UI_HIDDEN";
private static final String ACTION_TRIM_MEMORY_MODERATE =
"com.facebook.rnfeed.ACTION_TRIM_MEMORY_MODERATE";
private static final String ACTION_TRIM_MEMORY_CRITICAL =
@@ -52,6 +55,9 @@ public class MemoryPressureRouter {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static boolean handleDebugIntent(Application application, String action) {
switch (action) {
case ACTION_TRIM_MEMORY_UI_HIDDEN:
simulateTrimMemory(application, ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN);
break;
case ACTION_TRIM_MEMORY_MODERATE:
simulateTrimMemory(application, TRIM_MEMORY_MODERATE);
break;
@@ -87,10 +93,12 @@ public class MemoryPressureRouter {
}
private void trimMemory(int level) {
if (level >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) {
if (level >= TRIM_MEMORY_COMPLETE) {
dispatchMemoryPressure(MemoryPressure.CRITICAL);
} else if (level >= TRIM_MEMORY_BACKGROUND || level == TRIM_MEMORY_RUNNING_CRITICAL) {
dispatchMemoryPressure(MemoryPressure.MODERATE);
} else if (level == TRIM_MEMORY_UI_HIDDEN) {
dispatchMemoryPressure(MemoryPressure.UI_HIDDEN);
}
}

View File

@@ -3,6 +3,7 @@
package com.facebook.react.bridge;
public enum MemoryPressure {
UI_HIDDEN,
MODERATE,
CRITICAL
}

View File

@@ -58,6 +58,9 @@ public class ReactBridge extends Countable {
public void handleMemoryPressure(MemoryPressure level) {
switch (level) {
case UI_HIDDEN:
handleMemoryPressureUiHidden();
break;
case MODERATE:
handleMemoryPressureModerate();
break;
@@ -86,6 +89,7 @@ public class ReactBridge extends Countable {
public native void startProfiler(String title);
public native void stopProfiler(String title, String filename);
public native ExecutorToken getMainExecutorToken();
private native void handleMemoryPressureUiHidden();
private native void handleMemoryPressureModerate();
private native void handleMemoryPressureCritical();
public native void destroy();

View File

@@ -289,8 +289,26 @@ public class CatalystInstanceImpl implements CatalystInstance {
return mJavaRegistry.getAllModules();
}
private native void handleMemoryPressureUiHidden();
private native void handleMemoryPressureModerate();
private native void handleMemoryPressureCritical();
@Override
public void handleMemoryPressure(MemoryPressure level) {
if (mDestroyed) {
return;
}
switch(level) {
case UI_HIDDEN:
handleMemoryPressureUiHidden();
break;
case MODERATE:
handleMemoryPressureModerate();
break;
case CRITICAL:
handleMemoryPressureCritical();
break;
}
}
/**