Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dd6a0dbf authored by Richard Uhler's avatar Richard Uhler
Browse files

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
parent 04489c63
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -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;
    }