Permalink
Browse files

Simplify Catalyst handleMemoryPressure

Reviewed By: cwdick

Differential Revision: D5200555

fbshipit-source-id: 86f12acca33ece265d3482ba52de9afcc83173cd
  • Loading branch information...
javache authored and facebook-github-bot committed Jun 26, 2017
1 parent e2dff82 commit 83faa4b608293e5a8f653f34fa5b831b9e9090b3
@@ -25,32 +25,18 @@
/**
* 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 static boolean handleDebugIntent(Application application, String action)
}
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 void removeMemoryPressureListener(MemoryPressureListener listener) {
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 @@ private void trimMemory(int level) {
}
}
@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 ReactQueueConfiguration getReactQueueConfiguration() {
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);
@@ -134,17 +134,11 @@ void Instance::callJSCallback(uint64_t callbackId, folly::dynamic&& params) {
nativeToJsBridge_->invokeCallback((double) callbackId, std::move(params));
}
void Instance::handleMemoryPressureUiHidden() {
nativeToJsBridge_->handleMemoryPressureUiHidden();
}
void Instance::handleMemoryPressureModerate() {
nativeToJsBridge_->handleMemoryPressureModerate();
}
void Instance::handleMemoryPressureCritical() {
nativeToJsBridge_->handleMemoryPressureCritical();
#ifdef WITH_JSC_MEMORY_PRESSURE
void Instance::handleMemoryPressure(int pressureLevel) {
nativeToJsBridge_->handleMemoryPressure(pressureLevel);
}
#endif
} // namespace react
} // namespace facebook
@@ -63,9 +63,9 @@ class Instance {
return nativeToJsBridge_->callFunctionSync(module, method, std::forward<T>(args));
}
void handleMemoryPressureUiHidden();
void handleMemoryPressureModerate();
void handleMemoryPressureCritical();
#ifdef WITH_JSC_MEMORY_PRESSURE
void handleMemoryPressure(int pressureLevel);
#endif
private:
void callNativeModules(folly::dynamic&& calls, bool isEndOfBatch);
@@ -579,23 +579,11 @@ void JSCExecutor::stopProfiler(const std::string &titleString, const std::string
#endif
}
void JSCExecutor::handleMemoryPressureUiHidden() {
#ifdef WITH_JSC_MEMORY_PRESSURE
JSHandleMemoryPressure(this, m_context, JSMemoryPressure::UI_HIDDEN);
#endif
}
void JSCExecutor::handleMemoryPressureModerate() {
#ifdef WITH_JSC_MEMORY_PRESSURE
JSHandleMemoryPressure(this, m_context, JSMemoryPressure::MODERATE);
#endif
}
void JSCExecutor::handleMemoryPressureCritical() {
#ifdef WITH_JSC_MEMORY_PRESSURE
JSHandleMemoryPressure(this, m_context, JSMemoryPressure::CRITICAL);
#endif
#ifdef WITH_JSC_MEMORY_PRESSURE
void JSCExecutor::handleMemoryPressure(int pressureLevel) {
JSHandleMemoryPressure(this, m_context, static_cast<JSMemoryPressure>(pressureLevel));
}
#endif
void JSCExecutor::flushQueueImmediate(Value&& queue) {
auto queueStr = queue.toJSONString();
@@ -83,9 +83,9 @@ class RN_EXPORT JSCExecutor : public JSExecutor {
virtual void startProfiler(const std::string &titleString) override;
virtual void stopProfiler(const std::string &titleString, const std::string &filename) override;
virtual void handleMemoryPressureUiHidden() override;
virtual void handleMemoryPressureModerate() override;
virtual void handleMemoryPressureCritical() override;
#ifdef WITH_JSC_MEMORY_PRESSURE
virtual void handleMemoryPressure(int pressureLevel) override;
#endif
virtual void destroy() override;
@@ -77,11 +77,11 @@ class JSExecutor {
}
virtual void startProfiler(const std::string &titleString) {}
virtual void stopProfiler(const std::string &titleString, const std::string &filename) {}
virtual void handleMemoryPressureUiHidden() {}
virtual void handleMemoryPressureModerate() {}
virtual void handleMemoryPressureCritical() {
handleMemoryPressureModerate();
}
#ifdef WITH_JSC_MEMORY_PRESSURE
virtual void handleMemoryPressure(int pressureLevel) {}
#endif
virtual void destroy() {}
virtual ~JSExecutor() {}
};
@@ -201,23 +201,13 @@ void NativeToJsBridge::stopProfiler(const std::string& title, const std::string&
});
}
void NativeToJsBridge::handleMemoryPressureUiHidden() {
#ifdef WITH_JSC_MEMORY_PRESSURE
void NativeToJsBridge::handleMemoryPressure(int pressureLevel) {
runOnExecutorQueue([=] (JSExecutor* executor) {
executor->handleMemoryPressureUiHidden();
});
}
void NativeToJsBridge::handleMemoryPressureModerate() {
runOnExecutorQueue([=] (JSExecutor* executor) {
executor->handleMemoryPressureModerate();
});
}
void NativeToJsBridge::handleMemoryPressureCritical() {
runOnExecutorQueue([=] (JSExecutor* executor) {
executor->handleMemoryPressureCritical();
executor->handleMemoryPressure(pressureLevel);
});
}
#endif
void NativeToJsBridge::destroy() {
// All calls made through runOnExecutorQueue have an early exit if
@@ -103,9 +103,10 @@ class NativeToJsBridge {
bool supportsProfiling();
void startProfiler(const std::string& title);
void stopProfiler(const std::string& title, const std::string& filename);
void handleMemoryPressureUiHidden();
void handleMemoryPressureModerate();
void handleMemoryPressureCritical();
#ifdef WITH_JSC_MEMORY_PRESSURE
void handleMemoryPressure(int pressureLevel);
#endif
/**
* Synchronously tears down the bridge and the main executor.

0 comments on commit 83faa4b

Please sign in to comment.