From 3d9dad3b9b49e1b238889e9bae7d0b4e24a7d8fd Mon Sep 17 00:00:00 2001 From: vadimt Date: Mon, 2 Mar 2020 18:05:58 -0800 Subject: [PATCH] Saving heap dump immediately as reported by StrictMode Can do this via API with the exact heap state that triggered the callback because using API allows to avoid deadlock that we'd have in main thread if we used shell command. checkDetectedLeaks() is still needed, as I'll add throwing an exception there soon. Bug: 139137636 Change-Id: I3c8670071b058c5c146ac58bb16152c980553065 --- .../launcher3/ui/AbstractLauncherUiTest.java | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java index e93df96521..c6192bc7b7 100644 --- a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java +++ b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java @@ -35,9 +35,11 @@ import android.content.pm.ActivityInfo; import android.content.pm.LauncherActivityInfo; import android.content.pm.LauncherApps; import android.content.pm.PackageManager; +import android.os.Debug; import android.os.Process; import android.os.RemoteException; import android.os.StrictMode; +import android.util.Log; import androidx.test.InstrumentationRegistry; import androidx.test.uiautomator.By; @@ -117,6 +119,14 @@ public abstract class AbstractLauncherUiTest { // so let's just mark the fact that the leak has happened. if (sDetectedActivityLeak == null) { sDetectedActivityLeak = violation.toString(); + try { + Debug.dumpHprofData( + getInstrumentation().getTargetContext() + .getFilesDir().getPath() + + "/ActivityLeakHeapDump.hprof"); + } catch (IOException e) { + Log.e(TAG, "dumpHprofData failed", e); + } } }); StrictMode.setVmPolicy(builder.build()); @@ -126,18 +136,6 @@ public abstract class AbstractLauncherUiTest { public static void checkDetectedLeaks() { if (sDetectedActivityLeak != null && !sActivityLeakReported) { sActivityLeakReported = true; - - final UiDevice device = UiDevice.getInstance(getInstrumentation()); - try { - device.executeShellCommand( - "am dumpheap " - + device.getLauncherPackageName() - + " " - + getInstrumentation().getTargetContext().getFilesDir().getPath() - + "/ActivityLeakHeapDump.hprof"); - } catch (IOException e) { - throw new RuntimeException(e); - } } }