Skip to content

Commit 48001c5

Browse files
Riley Dulinfacebook-github-bot
authored andcommitted
Add call to collectGarbage for JSIExecutor::handleMemoryPressure
Summary: Someone pointed out in this Github issue: #27532 that the memory pressure warning from Android was being ignored, when it can easily be used to start a garbage collection on the JS runtime. Changelog: [Internal] Add a memory pressure handler for jsi::Runtime Reviewed By: mhorowitz Differential Revision: D20072943 fbshipit-source-id: 869a14068aa02bd378e8b26d8c18b76a5d0f7bc0
1 parent 22fdb8b commit 48001c5

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

ReactCommon/jsiexecutor/jsireact/JSIExecutor.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <folly/json.h>
1616
#include <glog/logging.h>
1717
#include <jsi/JSIDynamic.h>
18+
#include <jsi/instrumentation.h>
1819

1920
#include <sstream>
2021
#include <stdexcept>
@@ -273,6 +274,74 @@ bool JSIExecutor::isInspectable() {
273274
return runtime_->isInspectable();
274275
}
275276

277+
void JSIExecutor::handleMemoryPressure(int pressureLevel) {
278+
// The level is an enum value passed by the Android OS to an onTrimMemory
279+
// event callback. Defined in ComponentCallbacks2.
280+
enum AndroidMemoryPressure {
281+
TRIM_MEMORY_BACKGROUND = 40,
282+
TRIM_MEMORY_COMPLETE = 80,
283+
TRIM_MEMORY_MODERATE = 60,
284+
TRIM_MEMORY_RUNNING_CRITICAL = 15,
285+
TRIM_MEMORY_RUNNING_LOW = 10,
286+
TRIM_MEMORY_RUNNING_MODERATE = 5,
287+
TRIM_MEMORY_UI_HIDDEN = 20,
288+
};
289+
const char *levelName;
290+
switch (pressureLevel) {
291+
case TRIM_MEMORY_BACKGROUND:
292+
levelName = "TRIM_MEMORY_BACKGROUND";
293+
break;
294+
case TRIM_MEMORY_COMPLETE:
295+
levelName = "TRIM_MEMORY_COMPLETE";
296+
break;
297+
case TRIM_MEMORY_MODERATE:
298+
levelName = "TRIM_MEMORY_MODERATE";
299+
break;
300+
case TRIM_MEMORY_RUNNING_CRITICAL:
301+
levelName = "TRIM_MEMORY_RUNNING_CRITICAL";
302+
break;
303+
case TRIM_MEMORY_RUNNING_LOW:
304+
levelName = "TRIM_MEMORY_RUNNING_LOW";
305+
break;
306+
case TRIM_MEMORY_RUNNING_MODERATE:
307+
levelName = "TRIM_MEMORY_RUNNING_MODERATE";
308+
break;
309+
case TRIM_MEMORY_UI_HIDDEN:
310+
levelName = "TRIM_MEMORY_UI_HIDDEN";
311+
break;
312+
default:
313+
levelName = "UNKNOWN";
314+
break;
315+
}
316+
317+
switch (pressureLevel) {
318+
case TRIM_MEMORY_RUNNING_LOW:
319+
case TRIM_MEMORY_RUNNING_MODERATE:
320+
case TRIM_MEMORY_UI_HIDDEN:
321+
// For non-severe memory trims, do nothing.
322+
LOG(INFO) << "Memory warning (pressure level: " << levelName
323+
<< ") received by JS VM, ignoring because it's non-severe";
324+
break;
325+
case TRIM_MEMORY_BACKGROUND:
326+
case TRIM_MEMORY_COMPLETE:
327+
case TRIM_MEMORY_MODERATE:
328+
case TRIM_MEMORY_RUNNING_CRITICAL:
329+
// For now, pressureLevel is unused by collectGarbage.
330+
// This may change in the future if the JS GC has different styles of
331+
// collections.
332+
LOG(INFO) << "Memory warning (pressure level: " << levelName
333+
<< ") received by JS VM, running a GC";
334+
runtime_->instrumentation().collectGarbage();
335+
break;
336+
default:
337+
// Use the raw number instead of the name here since the name is
338+
// meaningless.
339+
LOG(WARNING) << "Memory warning (pressure level: " << pressureLevel
340+
<< ") received by JS VM, unrecognized pressure level";
341+
break;
342+
}
343+
}
344+
276345
void JSIExecutor::bindBridge() {
277346
std::call_once(bindFlag_, [this] {
278347
SystraceSection s("JSIExecutor::bindBridge (once)");

ReactCommon/jsiexecutor/jsireact/JSIExecutor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class JSIExecutor : public JSExecutor {
9595
std::string getDescription() override;
9696
void *getJavaScriptContext() override;
9797
bool isInspectable() override;
98+
void handleMemoryPressure(int pressureLevel) override;
9899

99100
// An implementation of JSIScopedTimeoutInvoker that simply runs the
100101
// invokee, with no timeout.

0 commit comments

Comments
 (0)