From 576bba9bb0bfc841f902b8ca58f2ff09374666ff Mon Sep 17 00:00:00 2001 From: Ming-Shin Lu Date: Thu, 28 Apr 2022 17:15:17 +0000 Subject: [PATCH] Fix memory usage issue on WindowTokenClient As WindowTokenClient#{onConfigurationChanged, onWindowTokenRemoved} requires to post on mainthread, in case posting runnable accquires more java objects to increase java heap memory, use PooledLambda to obtain cached runnable for saving memory usage. Also, use ActivityThread.currentActivityThread.getHandler to get the main handler rather than creating the new object. Fix: 215447496 Test: run MemoryOnBoot Change-Id: Idb2c01a0da02ff669e1a3f1a55f4841f7aa258ba --- .../android/window/WindowTokenClient.java | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/core/java/android/window/WindowTokenClient.java b/core/java/android/window/WindowTokenClient.java index 7db4243d3a83e..0976f45c02b03 100644 --- a/core/java/android/window/WindowTokenClient.java +++ b/core/java/android/window/WindowTokenClient.java @@ -21,8 +21,10 @@ import static android.window.ConfigurationHelper.shouldUpdateResources; import android.annotation.AnyThread; import android.annotation.BinderThread; +import android.annotation.MainThread; import android.annotation.NonNull; import android.annotation.Nullable; +import android.app.ActivityThread; import android.app.IWindowToken; import android.app.ResourcesManager; import android.content.Context; @@ -33,7 +35,6 @@ import android.os.Bundle; import android.os.Debug; import android.os.Handler; import android.os.IBinder; -import android.os.Looper; import android.os.RemoteException; import android.util.Log; import android.view.IWindowManager; @@ -42,6 +43,7 @@ import android.view.WindowManagerGlobal; import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.VisibleForTesting; +import com.android.internal.util.function.pooled.PooledLambda; import java.lang.ref.WeakReference; @@ -76,7 +78,7 @@ public class WindowTokenClient extends IWindowToken.Stub { private boolean mAttachToWindowContainer; - private final Handler mHandler = new Handler(Looper.getMainLooper()); + private final Handler mHandler = ActivityThread.currentActivityThread().getHandler(); /** * Attaches {@code context} to this {@link WindowTokenClient}. Each {@link WindowTokenClient} @@ -188,8 +190,8 @@ public class WindowTokenClient extends IWindowToken.Stub { @BinderThread @Override public void onConfigurationChanged(Configuration newConfig, int newDisplayId) { - mHandler.post(() -> onConfigurationChanged(newConfig, newDisplayId, - true /* shouldReportConfigChange */)); + mHandler.post(PooledLambda.obtainRunnable(this::onConfigurationChanged, newConfig, + newDisplayId, true /* shouldReportConfigChange */).recycleOnUse()); } // TODO(b/192048581): rewrite this method based on WindowContext and WindowProviderService @@ -279,12 +281,16 @@ public class WindowTokenClient extends IWindowToken.Stub { @BinderThread @Override public void onWindowTokenRemoved() { - mHandler.post(() -> { - final Context context = mContextRef.get(); - if (context != null) { - context.destroy(); - mContextRef.clear(); - } - }); + mHandler.post(PooledLambda.obtainRunnable( + WindowTokenClient::onWindowTokenRemovedInner, this).recycleOnUse()); + } + + @MainThread + private void onWindowTokenRemovedInner() { + final Context context = mContextRef.get(); + if (context != null) { + context.destroy(); + mContextRef.clear(); + } } }