Merge "Fix leak of WeakReferences on mThemeRefs list." into pi-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
7790f2a4e1
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user