forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trigger GC and drop compiled code on low memory
Reviewed By: astreet Differential Revision: D2658693 fb-gh-sync-id: 8cba49b67ac45a2dbf8b4c9c404d6fb9c97693f6
- Loading branch information
Showing
12 changed files
with
182 additions
and
3 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
ReactAndroid/src/main/java/com/facebook/react/MemoryPressureRouter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
package com.facebook.react; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import android.annotation.TargetApi; | ||
import android.app.Activity; | ||
import android.content.ComponentCallbacks2; | ||
import android.content.Context; | ||
import android.content.res.Configuration; | ||
import android.os.Build; | ||
|
||
import com.facebook.react.bridge.CatalystInstance; | ||
import com.facebook.react.bridge.MemoryPressure; | ||
import com.facebook.react.bridge.ReactContext; | ||
|
||
import static android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; | ||
import static android.content.ComponentCallbacks2.TRIM_MEMORY_MODERATE; | ||
import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL; | ||
|
||
/** | ||
* Translates and routes memory pressure events to the current catalyst instance. | ||
*/ | ||
public class MemoryPressureRouter { | ||
// Trigger this by sending an intent to your activity with adb shell: | ||
// am start -a "com.facebook.catalyst.ACTION_TRIM_MEMORY" --activity-single-top -n <activity> | ||
private static final String ACTION_TRIM_MEMORY ="com.facebook.catalyst.ACTION_TRIM_MEMORY"; | ||
|
||
private @Nullable CatalystInstance mCatalystInstance; | ||
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(Activity activity, String action) { | ||
switch (action) { | ||
case ACTION_TRIM_MEMORY: | ||
simulateTrimMemory(activity, TRIM_MEMORY_MODERATE); | ||
break; | ||
default: | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
MemoryPressureRouter(Context context) { | ||
context.getApplicationContext().registerComponentCallbacks(mCallbacks); | ||
} | ||
|
||
public void onNewReactContextCreated(ReactContext reactContext) { | ||
mCatalystInstance = reactContext.getCatalystInstance(); | ||
} | ||
|
||
public void onReactInstanceDestroyed() { | ||
mCatalystInstance = null; | ||
} | ||
|
||
public void destroy(Context context) { | ||
context.getApplicationContext().unregisterComponentCallbacks(mCallbacks); | ||
} | ||
|
||
private void trimMemory(int level) { | ||
if (level >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) { | ||
dispatchMemoryPressure(MemoryPressure.CRITICAL); | ||
} else if (level >= TRIM_MEMORY_BACKGROUND || level == TRIM_MEMORY_RUNNING_CRITICAL) { | ||
dispatchMemoryPressure(MemoryPressure.MODERATE); | ||
} | ||
} | ||
|
||
private void dispatchMemoryPressure(MemoryPressure level) { | ||
if (mCatalystInstance != null) { | ||
mCatalystInstance.handleMemoryPressure(level); | ||
} | ||
} | ||
|
||
private static void simulateTrimMemory(Activity activity, int level) { | ||
activity.getApplication().onTrimMemory(level); | ||
activity.onTrimMemory(level); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
ReactAndroid/src/main/java/com/facebook/react/bridge/MemoryPressure.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
package com.facebook.react.bridge; | ||
|
||
public enum MemoryPressure { | ||
MODERATE, | ||
CRITICAL | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters