Fix leak of WeakReferences on mThemeRefs list.

Periodically remove references from the list whose referents have been
garbage collected.

Bug: 73961798
Test: Device boots.
Test: Take a heap dump of systemui and manually check that the state of
      ThemeRefs looks reasonable.

Change-Id: I691027feb5dd217bcb60406b28897b9614e2a845
This commit is contained in:
Richard Uhler
2018-04-26 15:09:30 +01:00
parent 04489c6320
commit dd6a0dbf2f

View File

@@ -127,6 +127,14 @@ public class Resources {
*/
private final ArrayList<WeakReference<Theme>> mThemeRefs = new ArrayList<>();
/**
* To avoid leaking WeakReferences to garbage collected Themes on the
* mThemeRefs list, we flush the list of stale references any time the
* mThemeRefNextFlushSize is reached.
*/
private static final int MIN_THEME_REFS_FLUSH_SIZE = 32;
private int mThemeRefsNextFlushSize = MIN_THEME_REFS_FLUSH_SIZE;
/**
* Returns the most appropriate default theme for the specified target SDK version.
* <ul>
@@ -1770,6 +1778,13 @@ public class Resources {
theme.setImpl(mResourcesImpl.newThemeImpl());
synchronized (mThemeRefs) {
mThemeRefs.add(new WeakReference<>(theme));
// Clean up references to garbage collected themes
if (mThemeRefs.size() > mThemeRefsNextFlushSize) {
mThemeRefs.removeIf(ref -> ref.get() == null);
mThemeRefsNextFlushSize = Math.max(MIN_THEME_REFS_FLUSH_SIZE,
2 * mThemeRefs.size());
}
}
return theme;
}