Merge "Fix leak of WeakReferences on mThemeRefs list." into pi-dev

am: 7790f2a4e1

Change-Id: Ia6d0946520165519c2256ab0714a216ac495d45f
This commit is contained in:
Richard Uhler
2018-05-03 06:13:07 -07:00
committed by android-build-merger

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;
}