From f2fc14ec4c092614990804c952fb448c438688dc Mon Sep 17 00:00:00 2001 From: helencheuk Date: Fri, 24 Nov 2023 14:06:17 +0000 Subject: [PATCH] Remove excess entry before putting new item It is to fix gpu memory usage increase. Remove excess entry first so its max memory usage should not increase Fix: 309182285 Flag: ACONFIG com.android.launcher3.enable_grid_only_overview TEAMFOOD Test: N/A Change-Id: I40b4ec50c09001e6ee9d012bdcdd1444230daac3 --- .../util/TaskKeyByLastActiveTimeCache.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/quickstep/src/com/android/quickstep/util/TaskKeyByLastActiveTimeCache.java b/quickstep/src/com/android/quickstep/util/TaskKeyByLastActiveTimeCache.java index 79ca076a8a..21c9e09b10 100644 --- a/quickstep/src/com/android/quickstep/util/TaskKeyByLastActiveTimeCache.java +++ b/quickstep/src/com/android/quickstep/util/TaskKeyByLastActiveTimeCache.java @@ -44,7 +44,7 @@ public class TaskKeyByLastActiveTimeCache implements TaskKeyCache { private final PriorityQueue mQueue; public TaskKeyByLastActiveTimeCache(int maxSize) { - mMap = new HashMap(maxSize); + mMap = new HashMap(0); mQueue = new PriorityQueue<>(Comparator.comparingLong(t -> t.lastActiveTime)); mMaxSize = new AtomicInteger(maxSize); } @@ -106,7 +106,8 @@ public class TaskKeyByLastActiveTimeCache implements TaskKeyCache { } /** - * Adds an entry to the cache, optionally evicting the last accessed entry + * Adds an entry to the cache, optionally evicting the last accessed entry excluding the newly + * added entry */ @Override public final synchronized void put(Task.TaskKey key, V value) { @@ -117,9 +118,9 @@ public class TaskKeyByLastActiveTimeCache implements TaskKeyCache { mQueue.remove(entry.mKey); } + removeExcessIfNeeded(mMaxSize.get() - 1); mMap.put(key.id, new Entry<>(key, value)); mQueue.add(key); - removeExcessIfNeeded(); } else { Log.e(TAG, "Unexpected null key or value: " + key + ", " + value); } @@ -143,11 +144,11 @@ public class TaskKeyByLastActiveTimeCache implements TaskKeyCache { @Override public synchronized void updateCacheSizeAndRemoveExcess(int cacheSize) { mMaxSize.compareAndSet(mMaxSize.get(), cacheSize); - removeExcessIfNeeded(); + removeExcessIfNeeded(mMaxSize.get()); } - private synchronized void removeExcessIfNeeded() { - while (mQueue.size() > mMaxSize.get() && !mQueue.isEmpty()) { + private synchronized void removeExcessIfNeeded(int maxSize) { + while (mQueue.size() > maxSize && !mQueue.isEmpty()) { Task.TaskKey key = mQueue.poll(); mMap.remove(key.id); }