Delay gargabe collection during onDestroy()

Allow the activity object to become unreachable before
iniating a gargabe collection to sanitize memory.

Bug: 189315376
Test: add device lock, confirm device lock in Settings,
      then a memory dump and search for password shards.
Change-Id: I807d74628e1355814807855ad309d86ca1fb38a1
This commit is contained in:
Rubin Xu
2021-07-02 14:32:03 +01:00
parent cf5bb13576
commit 8e9d78ff91
2 changed files with 20 additions and 10 deletions

View File

@@ -18,6 +18,8 @@ package com.android.settings.password;
import android.app.KeyguardManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.UserManager;
import android.util.Log;
import android.view.MenuItem;
@@ -163,11 +165,14 @@ public abstract class ConfirmDeviceCredentialBaseActivity extends SettingsActivi
@Override
public void onDestroy() {
super.onDestroy();
// Force a garbage collection immediately to remove remnant of user password shards
// from memory.
System.gc();
System.runFinalization();
System.gc();
// Force a garbage collection to remove remnant of user password shards from memory.
// Execute this with a slight delay to allow the activity lifecycle to complete and
// the instance to become gc-able.
new Handler(Looper.myLooper()).postDelayed(() -> {
System.gc();
System.runFinalization();
System.gc();
}, 5000);
}
@Override