mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-23 11:57:46 +08:00
Simplify Catalyst handleMemoryPressure
Reviewed By: cwdick Differential Revision: D5200555 fbshipit-source-id: 86f12acca33ece265d3482ba52de9afcc83173cd
This commit is contained in:
committed by
Facebook Github Bot
parent
e2dff82160
commit
83faa4b608
@@ -25,32 +25,18 @@ import static android.content.ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN;
|
||||
/**
|
||||
* Translates and routes memory pressure events to the current catalyst instance.
|
||||
*/
|
||||
public class MemoryPressureRouter {
|
||||
public class MemoryPressureRouter implements ComponentCallbacks2 {
|
||||
// Trigger this by sending an intent to your activity with adb shell:
|
||||
// am broadcast -a com.facebook.catalyst.ACTION_TRIM_MEMORY_MODERATE
|
||||
// am broadcast -a com.facebook.react.ACTION_TRIM_MEMORY_MODERATE
|
||||
private static final String ACTION_TRIM_MEMORY_UI_HIDDEN =
|
||||
"com.facebook.rnfeed.ACTION_TRIM_MEMORY_UI_HIDDEN";
|
||||
"com.facebook.react.ACTION_TRIM_MEMORY_UI_HIDDEN";
|
||||
private static final String ACTION_TRIM_MEMORY_MODERATE =
|
||||
"com.facebook.rnfeed.ACTION_TRIM_MEMORY_MODERATE";
|
||||
"com.facebook.react.ACTION_TRIM_MEMORY_MODERATE";
|
||||
private static final String ACTION_TRIM_MEMORY_CRITICAL =
|
||||
"com.facebook.rnfeed.ACTION_TRIM_MEMORY_CRITICAL";
|
||||
"com.facebook.react.ACTION_TRIM_MEMORY_CRITICAL";
|
||||
|
||||
private final Set<MemoryPressureListener> mListeners =
|
||||
Collections.synchronizedSet(new LinkedHashSet<MemoryPressureListener>());
|
||||
private final ComponentCallbacks2 mCallbacks = new ComponentCallbacks2() {
|
||||
@Override
|
||||
public void onTrimMemory(int level) {
|
||||
trimMemory(level);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConfigurationChanged(Configuration newConfig) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLowMemory() {
|
||||
}
|
||||
};
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
|
||||
public static boolean handleDebugIntent(Application application, String action) {
|
||||
@@ -71,7 +57,11 @@ public class MemoryPressureRouter {
|
||||
}
|
||||
|
||||
MemoryPressureRouter(Context context) {
|
||||
context.getApplicationContext().registerComponentCallbacks(mCallbacks);
|
||||
context.getApplicationContext().registerComponentCallbacks(this);
|
||||
}
|
||||
|
||||
public void destroy(Context context) {
|
||||
context.getApplicationContext().unregisterComponentCallbacks(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,11 +78,8 @@ public class MemoryPressureRouter {
|
||||
mListeners.remove(listener);
|
||||
}
|
||||
|
||||
public void destroy(Context context) {
|
||||
context.getApplicationContext().unregisterComponentCallbacks(mCallbacks);
|
||||
}
|
||||
|
||||
private void trimMemory(int level) {
|
||||
@Override
|
||||
public void onTrimMemory(int level) {
|
||||
if (level >= TRIM_MEMORY_COMPLETE) {
|
||||
dispatchMemoryPressure(MemoryPressure.CRITICAL);
|
||||
} else if (level >= TRIM_MEMORY_BACKGROUND || level == TRIM_MEMORY_RUNNING_CRITICAL) {
|
||||
@@ -102,6 +89,14 @@ public class MemoryPressureRouter {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConfigurationChanged(Configuration newConfig) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLowMemory() {
|
||||
}
|
||||
|
||||
private void dispatchMemoryPressure(MemoryPressure level) {
|
||||
// copy listeners array to avoid ConcurrentModificationException if any of the listeners remove
|
||||
// themselves in handleMemoryPressure()
|
||||
|
||||
@@ -393,26 +393,14 @@ public class CatalystInstanceImpl implements CatalystInstance {
|
||||
return mNativeModuleRegistry.getAllModules();
|
||||
}
|
||||
|
||||
private native void handleMemoryPressureUiHidden();
|
||||
private native void handleMemoryPressureModerate();
|
||||
private native void handleMemoryPressureCritical();
|
||||
private native void jniHandleMemoryPressure(int level);
|
||||
|
||||
@Override
|
||||
public void handleMemoryPressure(MemoryPressure level) {
|
||||
if (mDestroyed) {
|
||||
return;
|
||||
}
|
||||
switch (level) {
|
||||
case UI_HIDDEN:
|
||||
handleMemoryPressureUiHidden();
|
||||
break;
|
||||
case MODERATE:
|
||||
handleMemoryPressureModerate();
|
||||
break;
|
||||
case CRITICAL:
|
||||
handleMemoryPressureCritical();
|
||||
break;
|
||||
}
|
||||
jniHandleMemoryPressure(level.ordinal());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -40,6 +40,7 @@ cxx_library(
|
||||
"-DLOG_TAG=\"ReactNativeJNI\"",
|
||||
"-DWITH_FBSYSTRACE=1",
|
||||
"-DWITH_INSPECTOR=1",
|
||||
"-DWITH_JSC_MEMORY_PRESSURE=1",
|
||||
],
|
||||
soname = "libreactnativejni.$(ext)",
|
||||
visibility = [
|
||||
|
||||
@@ -103,9 +103,7 @@ void CatalystInstanceImpl::registerNatives() {
|
||||
makeNativeMethod("jniCallJSCallback", CatalystInstanceImpl::jniCallJSCallback),
|
||||
makeNativeMethod("setGlobalVariable", CatalystInstanceImpl::setGlobalVariable),
|
||||
makeNativeMethod("getJavaScriptContext", CatalystInstanceImpl::getJavaScriptContext),
|
||||
makeNativeMethod("handleMemoryPressureUiHidden", CatalystInstanceImpl::handleMemoryPressureUiHidden),
|
||||
makeNativeMethod("handleMemoryPressureModerate", CatalystInstanceImpl::handleMemoryPressureModerate),
|
||||
makeNativeMethod("handleMemoryPressureCritical", CatalystInstanceImpl::handleMemoryPressureCritical),
|
||||
makeNativeMethod("jniHandleMemoryPressure", CatalystInstanceImpl::handleMemoryPressure),
|
||||
makeNativeMethod("supportsProfiling", CatalystInstanceImpl::supportsProfiling),
|
||||
makeNativeMethod("startProfiler", CatalystInstanceImpl::startProfiler),
|
||||
makeNativeMethod("stopProfiler", CatalystInstanceImpl::stopProfiler),
|
||||
@@ -262,16 +260,10 @@ jlong CatalystInstanceImpl::getJavaScriptContext() {
|
||||
return (jlong) (intptr_t) instance_->getJavaScriptContext();
|
||||
}
|
||||
|
||||
void CatalystInstanceImpl::handleMemoryPressureUiHidden() {
|
||||
instance_->handleMemoryPressureUiHidden();
|
||||
}
|
||||
|
||||
void CatalystInstanceImpl::handleMemoryPressureModerate() {
|
||||
instance_->handleMemoryPressureModerate();
|
||||
}
|
||||
|
||||
void CatalystInstanceImpl::handleMemoryPressureCritical() {
|
||||
instance_->handleMemoryPressureCritical();
|
||||
void CatalystInstanceImpl::handleMemoryPressure(int pressureLevel) {
|
||||
#ifdef WITH_JSC_MEMORY_PRESSURE
|
||||
instance_->handleMemoryPressure(pressureLevel);
|
||||
#endif
|
||||
}
|
||||
|
||||
jboolean CatalystInstanceImpl::supportsProfiling() {
|
||||
|
||||
@@ -68,9 +68,7 @@ class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {
|
||||
void setGlobalVariable(std::string propName,
|
||||
std::string&& jsonValue);
|
||||
jlong getJavaScriptContext();
|
||||
void handleMemoryPressureUiHidden();
|
||||
void handleMemoryPressureModerate();
|
||||
void handleMemoryPressureCritical();
|
||||
void handleMemoryPressure(int pressureLevel);
|
||||
jboolean supportsProfiling();
|
||||
void startProfiler(const std::string& title);
|
||||
void stopProfiler(const std::string& title, const std::string& filename);
|
||||
|
||||
Reference in New Issue
Block a user