Java Code Examples for android.os.Debug#dumpHprofData()

The following examples show how to use android.os.Debug#dumpHprofData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DdmHandleHeap.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private Chunk handleHPDU(Chunk request) {
    ByteBuffer in = wrapChunk(request);
    byte result;

    /* get the filename for the output file */
    int len = in.getInt();
    String fileName = getString(in, len);
    if (false)
        Log.d("ddm-heap", "Heap dump: file='" + fileName + "'");

    try {
        Debug.dumpHprofData(fileName);
        result = 0;
    } catch (UnsupportedOperationException uoe) {
        Log.w("ddm-heap", "hprof dumps not supported in this VM");
        result = -1;
    } catch (IOException ioe) {
        result = -1;
    } catch (RuntimeException re) {
        result = -1;
    }

    /* create a non-empty reply so the handler fires on completion */
    byte[] reply = { result };
    return new Chunk(CHUNK_HPDU, reply, 0, reply.length);
}
 
Example 2
Source File: AndroidHeapDumper.java    From leakcanary-for-eclipse with MIT License 6 votes vote down vote up
@Override
public File dumpHeap() {
   if (!isExternalStorageWritable()) {
     Log.d(TAG, "Could not dump heap, external storage not mounted.");
   }
   File heapDumpFile = getHeapDumpFile();
   if (heapDumpFile.exists()) {
     Log.d(TAG, "Could not dump heap, previous analysis still is in progress.");
     // Heap analysis in progress, let's not put too much pressure on the device.
     return null;
   }
   try {
     Debug.dumpHprofData(heapDumpFile.getAbsolutePath());
     return heapDumpFile;
   } catch (IOException e) {
     cleanup();
     Log.e(TAG, "Could not perform heap dump", e);
     // Abort heap dump
     return null;
   }
 }
 
Example 3
Source File: HeapDump.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
public static void dumpHeap(String filename) {
	try {
		Debug.dumpHprofData(filename);
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example 4
Source File: AndroidHeapDumper.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("ReferenceEquality") // Explicitly checking for named null.
@Override
@Nullable
public File dumpHeap() {
  File heapDumpFile = leakDirectoryProvider.newHeapDumpFile();

  if (heapDumpFile == RETRY_LATER) {
    return RETRY_LATER;
  }

  FutureResult<Toast> waitingForToast = new FutureResult<>();
  showToast(waitingForToast);

  if (!waitingForToast.wait(5, SECONDS)) {
    CanaryLog.d("Did not dump heap, too much time waiting for Toast.");
    return RETRY_LATER;
  }

  Notification.Builder builder = new Notification.Builder(context)
      .setContentTitle(context.getString(R.string.leak_canary_notification_dumping));
  Notification notification = LeakCanaryInternals.buildNotification(context, builder);
  NotificationManager notificationManager =
      (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  int notificationId = (int) SystemClock.uptimeMillis();
  notificationManager.notify(notificationId, notification);

  Toast toast = waitingForToast.get();
  try {
    Debug.dumpHprofData(heapDumpFile.getAbsolutePath());
    cancelToast(toast);
    notificationManager.cancel(notificationId);
    return heapDumpFile;
  } catch (Exception e) {
    CanaryLog.d(e, "Could not dump heap");
    // Abort heap dump
    return RETRY_LATER;
  }
}
 
Example 5
Source File: HeapDump.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
public static void dumpHeap(String filename) {
	try {
		Debug.dumpHprofData(filename);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
 
Example 6
Source File: HprofDumperPlugin.java    From weex with Apache License 2.0 5 votes vote down vote up
private void writeHprof(File outputPath) throws DumpException {
  try {
    // Test that we can write here.  dumpHprofData appears to hang if it cannot write
    // to the target location on ART.
    truncateAndDeleteFile(outputPath);
    Debug.dumpHprofData(outputPath.getAbsolutePath());
  } catch (IOException e) {
    throw new DumpException("Failure writing to " + outputPath + ": " + e.getMessage());
  }
}
 
Example 7
Source File: HeapDump.java    From AppTroy with Apache License 2.0 5 votes vote down vote up
public static void dumpHeap(String filename) {
    try {
        Debug.dumpHprofData(filename);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
 
Example 8
Source File: HprofDumperPlugin.java    From stetho with MIT License 5 votes vote down vote up
private void writeHprof(File outputPath) throws DumpException {
  try {
    // Test that we can write here.  dumpHprofData appears to hang if it cannot write
    // to the target location on ART.
    truncateAndDeleteFile(outputPath);
    Debug.dumpHprofData(outputPath.getAbsolutePath());
  } catch (IOException e) {
    throw new DumpException("Failure writing to " + outputPath + ": " + e.getMessage());
  }
}
 
Example 9
Source File: HeapDump.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
public static void dumpHeap(String filename) {
	try {
		Debug.dumpHprofData(filename);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
 
Example 10
Source File: HprofCatcher.java    From hprof-tools with MIT License 5 votes vote down vote up
@Override
public void uncaughtException(Thread thread, Throwable ex) {
    try {
        String fileName = mContext.getFilesDir().getAbsolutePath() + "/" + System.currentTimeMillis() + ".hprof";
        Log.d(TAG, "Writing memory dump to: " + fileName);
        Debug.dumpHprofData(fileName);
    }
    catch (Throwable t) {
        // Make sure we don't throw any new exception here!
        Log.e(TAG, "Failed to write memory dump", t);
    }
    mOldHandler.uncaughtException(thread, ex);
}
 
Example 11
Source File: HeapDump.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
public static void dumpHeap(String filename) {
	try {
		Debug.dumpHprofData(filename);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
 
Example 12
Source File: InstrumentationLeakDetector.java    From DoraemonKit with Apache License 2.0 4 votes vote down vote up
public @NonNull InstrumentationLeakResults detectLeaks() {
  Instrumentation instrumentation = getInstrumentation();
  Context context = instrumentation.getTargetContext();
  RefWatcher refWatcher = LeakCanary.installedRefWatcher();
  Set<String> retainedKeys = refWatcher.getRetainedKeys();

  if (refWatcher.isEmpty()) {
    return InstrumentationLeakResults.NONE;
  }

  instrumentation.waitForIdleSync();
  if (refWatcher.isEmpty()) {
    return InstrumentationLeakResults.NONE;
  }

  GcTrigger.DEFAULT.runGc();
  if (refWatcher.isEmpty()) {
    return InstrumentationLeakResults.NONE;
  }

  // Waiting for any delayed UI post (e.g. scroll) to clear. This shouldn't be needed, but
  // Android simply has way too many delayed posts that aren't canceled when views are detached.
  SystemClock.sleep(2000);

  if (refWatcher.isEmpty()) {
    return InstrumentationLeakResults.NONE;
  }

  // Aaand we wait some more.
  // 4 seconds (2+2) is greater than the 3 seconds delay for
  // FINISH_TOKEN in android.widget.Filter
  SystemClock.sleep(2000);
  GcTrigger.DEFAULT.runGc();

  if (refWatcher.isEmpty()) {
    return InstrumentationLeakResults.NONE;
  }

  // We're always reusing the same file since we only execute this once at a time.
  File heapDumpFile = new File(context.getFilesDir(), "instrumentation_tests_heapdump.hprof");
  try {
    Debug.dumpHprofData(heapDumpFile.getAbsolutePath());
  } catch (Exception e) {
    CanaryLog.d(e, "Could not dump heap");
    return InstrumentationLeakResults.NONE;
  }

  HeapDump.Builder heapDumpBuilder = refWatcher.getHeapDumpBuilder();
  HeapAnalyzer heapAnalyzer =
      new HeapAnalyzer(heapDumpBuilder.excludedRefs, AnalyzerProgressListener.NONE,
          heapDumpBuilder.reachabilityInspectorClasses);

  List<TrackedReference> trackedReferences = heapAnalyzer.findTrackedReferences(heapDumpFile);

  List<InstrumentationLeakResults.Result> detectedLeaks = new ArrayList<>();
  List<InstrumentationLeakResults.Result> excludedLeaks = new ArrayList<>();
  List<InstrumentationLeakResults.Result> failures = new ArrayList<>();

  for (TrackedReference trackedReference : trackedReferences) {
    // Ignore any Weak Reference that this test does not care about.
    if (!retainedKeys.contains(trackedReference.key)) {
      continue;
    }

    HeapDump heapDump = HeapDump.builder()
        .heapDumpFile(heapDumpFile)
        .referenceKey(trackedReference.key)
        .referenceName(trackedReference.name)
        .excludedRefs(heapDumpBuilder.excludedRefs)
        .reachabilityInspectorClasses(heapDumpBuilder.reachabilityInspectorClasses)
        .build();

    AnalysisResult analysisResult =
        heapAnalyzer.checkForLeak(heapDumpFile, trackedReference.key, false);

    InstrumentationLeakResults.Result leakResult =
        new InstrumentationLeakResults.Result(heapDump, analysisResult);

    if (analysisResult.leakFound) {
      if (!analysisResult.excludedLeak) {
        detectedLeaks.add(leakResult);
      } else {
        excludedLeaks.add(leakResult);
      }
    } else if (analysisResult.failure != null) {
      failures.add(leakResult);
    }
  }

  CanaryLog.d("Found %d proper leaks, %d excluded leaks and %d leak analysis failures",
      detectedLeaks.size(),
      excludedLeaks.size(),
      failures.size());

  return new InstrumentationLeakResults(detectedLeaks, excludedLeaks, failures);
}