From aece57be291ebdcbab300a667480bc0454c15561 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Fri, 8 May 2020 17:30:24 -0700 Subject: [PATCH] Fabric: Calling JSVM GC on memory pressure event on iOS Summary: This change is especially important for Fabric when a lot of objects (mostly `ShadowNode`s) have shared ownership. Without this change, JSVM could not know that bunch of natively allocated objects should be deallocated. Changelog: [Internal] Fabric-specific internal change. Reviewed By: dulinriley Differential Revision: D21484773 fbshipit-source-id: 46e32de0f108082e60df346884c9287023156149 --- React/CxxBridge/RCTCxxBridge.mm | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/React/CxxBridge/RCTCxxBridge.mm b/React/CxxBridge/RCTCxxBridge.mm index ec4090a862c..4f2189c1ced 100644 --- a/React/CxxBridge/RCTCxxBridge.mm +++ b/React/CxxBridge/RCTCxxBridge.mm @@ -241,10 +241,20 @@ struct RCTInstanceCallback : public InstanceCallback { _moduleDataByID = [NSMutableArray new]; [RCTBridge setCurrentBridge:self]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handleMemoryWarning) + name:UIApplicationDidReceiveMemoryWarningNotification + object:nil]; } return self; } +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + + (void)runRunLoop { @autoreleasepool { @@ -278,6 +288,13 @@ struct RCTInstanceCallback : public InstanceCallback { } } +- (void)handleMemoryWarning +{ + if (_reactInstance) { + _reactInstance->handleMemoryPressure(15 /* TRIM_MEMORY_RUNNING_CRITICAL */); + } +} + /** * Ensure block is run on the JS thread. If we're already on the JS thread, the block will execute synchronously. * If we're not on the JS thread, the block is dispatched to that thread. Any errors encountered while executing