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

Commit 576bba9b authored by Ming-Shin Lu's avatar Ming-Shin Lu
Browse files

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
parent c0573c98
Loading
Loading
Loading
Loading
+17 −11
Original line number Diff line number Diff line
@@ -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(() -> {
        mHandler.post(PooledLambda.obtainRunnable(
                WindowTokenClient::onWindowTokenRemovedInner, this).recycleOnUse());
    }

    @MainThread
    private void onWindowTokenRemovedInner() {
        final Context context = mContextRef.get();
        if (context != null) {
            context.destroy();
            mContextRef.clear();
        }
        });
    }
}