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

Commit ce8db89c authored by Wale Ogunwale's avatar Wale Ogunwale Committed by Garfield Tan
Browse files

Fixed thread leak in OomAdjuster during tests

OomAdjuster creates a thread in its ctor which is okay during normal
operation since we only call the ctor once. However, during tests it is
called per test setup, so we end up with a memory leak due to the
continuous creation of the thread.
We fix this by passing in the handler thread we use during testing into
the ctor since there is no need for a separate thread.

Test: presubmit
Bug: 137879065
Change-Id: Ia5664b3a4aa433a9fd55b714cc2a556c4e1b4eaa
parent 60ca0dbe
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -2399,7 +2399,7 @@ public class ActivityManagerService extends IActivityManager.Stub
        final ActiveUids activeUids = new ActiveUids(this, false /* postChangesToAtm */);
        mProcessList.init(this, activeUids);
        mLowMemDetector = null;
        mOomAdjuster = new OomAdjuster(this, mProcessList, activeUids);
        mOomAdjuster = new OomAdjuster(this, mProcessList, activeUids, handlerThread);
        mIntentFirewall = hasHandlerThread
                ? new IntentFirewall(new IntentFirewallInterface(), mHandler) : null;
+15 −6
Original line number Diff line number Diff line
@@ -162,6 +162,21 @@ public final class OomAdjuster {
    private final ProcessList mProcessList;

    OomAdjuster(ActivityManagerService service, ProcessList processList, ActiveUids activeUids) {
        this(service, processList, activeUids, createAdjusterThread());
    }

    private static ServiceThread createAdjusterThread() {
        // The process group is usually critical to the response time of foreground app, so the
        // setter should apply it as soon as possible.
        final ServiceThread adjusterThread =
                new ServiceThread(TAG, TOP_APP_PRIORITY_BOOST, false /* allowIo */);
        adjusterThread.start();
        Process.setThreadGroupAndCpuset(adjusterThread.getThreadId(), THREAD_GROUP_TOP_APP);
        return adjusterThread;
    }

    OomAdjuster(ActivityManagerService service, ProcessList processList, ActiveUids activeUids,
            ServiceThread adjusterThread) {
        mService = service;
        mProcessList = processList;
        mActiveUids = activeUids;
@@ -170,12 +185,6 @@ public final class OomAdjuster {
        mConstants = mService.mConstants;
        mAppCompact = new AppCompactor(mService);

        // The process group is usually critical to the response time of foreground app, so the
        // setter should apply it as soon as possible.
        final ServiceThread adjusterThread = new ServiceThread(TAG, TOP_APP_PRIORITY_BOOST,
                false /* allowIo */);
        adjusterThread.start();
        Process.setThreadGroupAndCpuset(adjusterThread.getThreadId(), THREAD_GROUP_TOP_APP);
        mProcessGroupHandler = new Handler(adjusterThread.getLooper(), msg -> {
            final int pid = msg.arg1;
            final int group = msg.arg2;