Clean up View Recycling

Summary:
Followups to View Recycling diffs to improve things / clean up things a bit. This also fixes memory warnings which were not hooked up before.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D36707792

fbshipit-source-id: 410e70bf0eeec5569566138af547e1601394d0e6
This commit is contained in:
Joshua Gross authored and Facebook GitHub Bot committed 2022-05-31 14:34:33 -07:00
1 parent bffad4351c
commit a68dca3c46
9 files changed
+101 -107

No files matched your search

@@ -19,11 +19,10 @@ import java.util.Map;
* Class that stores the mapping between native view name used in JS and the corresponding instance
* of {@link ViewManager}.
*/
public final class ViewManagerRegistry {
public final class ViewManagerRegistry implements ComponentCallbacks2 {
private final Map<String, ViewManager> mViewManagers;
private final @Nullable ViewManagerResolver mViewManagerResolver;
private final MemoryTrimCallback mMemoryTrimCallback = new MemoryTrimCallback();
public ViewManagerRegistry(ViewManagerResolver viewManagerResolver) {
mViewManagers = MapBuilder.newHashMap();
@@ -46,40 +45,6 @@ public final class ViewManagerRegistry {
mViewManagerResolver = null;
}
/**
* Trim View Recycling memory aggressively. Whenever the system is running even slightly low on
* memory or is backgrounded, we immediately flush all recyclable Views. GC and memory swaps cause
* intense CPU pressure, so we always favor low memory usage over View recycling, even if there is
* only "moderate" pressure.
*/
private class MemoryTrimCallback implements ComponentCallbacks2 {
@Override
public void onTrimMemory(int level) {
Runnable runnable =
new Runnable() {
@Override
public void run() {
for (Map.Entry<String, ViewManager> entry : mViewManagers.entrySet()) {
entry.getValue().trimMemory();
}
}
};
if (UiThreadUtil.isOnUiThread()) {
runnable.run();
} else {
UiThreadUtil.runOnUiThread(runnable);
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {}
@Override
public void onLowMemory() {
this.onTrimMemory(0);
}
}
/**
* @param className {@link String} that identifies the {@link ViewManager} inside the {@link
* ViewManagerRegistry}. This methods {@throws IllegalViewOperationException} if there is no
@@ -147,4 +112,33 @@ public final class ViewManagerRegistry {
UiThreadUtil.runOnUiThread(runnable);
}
}
/** ComponentCallbacks2 method. */
@Override
public void onTrimMemory(int level) {
Runnable runnable =
new Runnable() {
@Override
public void run() {
for (Map.Entry<String, ViewManager> entry : mViewManagers.entrySet()) {
entry.getValue().trimMemory();
}
}
};
if (UiThreadUtil.isOnUiThread()) {
runnable.run();
} else {
UiThreadUtil.runOnUiThread(runnable);
}
}
/** ComponentCallbacks2 method. */
@Override
public void onConfigurationChanged(Configuration newConfig) {}
/** ComponentCallbacks2 method. */
@Override
public void onLowMemory() {
this.onTrimMemory(0);
}
}