Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

HPROF Viewer and Analyzer

This tool is deprecated.

You should get the latest version of Android Studio and use the new Memory Profiler instead.

When you're monitoring memory usage in the Memory Monitor included in Android Monitor you can, at the same time, dump a snapshot of the Java heap to an Android-specific Heap/CPU Profiling (HPROF) file. The HPROF Viewer displays classes, instances of each class, and a reference tree to help you track memory usage and find memory leaks. HPROF is a binary heap dump format originally supported by J2SE.

Why Look at the Java Heap?

The Java heap display does the following:

  • Shows snapshots of a number of objects allocated by type.
  • Samples data every time a garbage collection event occurs naturally or is triggered by you.
  • Helps identify which object types might be involved in memory leaks.

However, you have to look for changes over time yourself by tracking what's happening in the graph.

The HPROF Analyzer finds the following potential issues:

  • All destroyed activity instances that are reachable from garbage collection roots.
  • Where the target program has strings that repeat values.

A dominator is at the top of a tree. If you remove it, you also remove the branches of the tree it dominates, so it’s a potential way to free memory.

Understanding the HPROF Viewer display

The HPROF Viewer looks similar to the following figure:

The tool displays the following information:

Column Description
Class Name The Java class responsible for the memory.
Total Count Total number of instances outstanding.
Heap Count Number of instances in the selected heap.
Sizeof Size of the instances (currently, 0 if the size is variable).
Shallow Size Total size of all instances in this heap.
Retained Size Size of memory that all instances of this class is dominating.
Instance A specific instance of the class.
Reference Tree References that point to the selected instance, as well as references pointing to the references.
Depth The shortest number of hops from any GC root to the selected instance.
Shallow Size Size of this instance.
Dominating Size Size of memory that this instance is dominating.

If you click Analyzer Tasks, the HPROF Analyzer appears:

You can detect leaked activities and find duplicate strings with the HPROF Analyzer.

Taking and displaying a snapshot of the Java heap

To examine a snapshot of the Java heap:

  1. Display a running app in the Memory Monitor.
  2. Click Dump Java Heap Dump Java Heap icon.
  3. When the icon on the Memory Monitor display changes from Dump Java Heap Start icon to Dump Java Heap End icon, the file is ready. Android Studio creates the heap snapshot file with the filename using the activity package (or project) name, year, month, day, hour, minute, and second of the capture, for example,

    The HPROF Viewer appears.

Viewing a saved HPROF file

After you do a heap dump, Android Studio automatically stores it so you can view it again.

To view an HPROF file in the HPROF Viewer:

  1. Click Captures icon in the main window.
  2. Or select View > Tools Windows > Captures.

    The Captures window appears.

  3. Open the Heap Snapshot folder.
  4. Double-click the file to view it in the HPROF Viewer.
  5. The HPROF Viewer appears.

  6. Select the Heap menu option you want to display:
    • App heap - The heap used by the current app.
    • Image heap - The memory mapped copy of the current app on disk.
    • Zygote heap - The common set of libraries and runtime classes and data that all apps are forked from. The zygote space is created during device startup and is never allocated into.
  7. Select the View menu option you want to display:
    • Class List View
    • Package Tree View

Diving into heap dump data in the HPROF Viewer

The following steps outline the typical workflow:

  1. In the HPROF Viewer, select a class name.
  2. Select an instance of that class.
  3. Examine the reference tree.
  4. Right-click an item to Jump to source or Go to instance, as needed.

Analyzing heap dump data in the HPROF Analyzer

You can detect leaked activities and find duplicate strings with the HPROF Analyzer.

To use the HPROF Analyzer:

  1. In the Captures window, double-click an .hprof file to display it in the HPROF Viewer.
  2. Click Analyzer Tasks on the right side of the main Android Studio window.
  3. The HPROF Analyzer appears to the right of the HPROF Analyzer, by default.

  4. In the Analyzer Tasks list, select the items you want to find.
  5. Click Perform Analysis Perform Analysis icon.
  6. Examine the items in Analysis Results. Click an item to display it in the HPROF Viewer.

Sorting heap dump data

To sort heap dump data:

  • In the HPROF Viewer, click a column heading to sort the table by ascending or descending order.

Displaying Java source

For some items displayed in the HPROF Viewer, you can go straight to its source code.

To display Java source:

  • In the HPROF Viewer, right-click a class, instance, or item in the reference tree, and then select Jump to Source.
  • The source code appears in the Code Editor.

Working with HPROF files

You can rename, locate, and delete an HPROF file from within Android Studio. You can also convert it to standard HPROF format to use with other analysis tools.

Renaming an HPROF file

If you rename a file from within Android Studio, it continues to appear in the Captures window.

To rename an HPROF file:

  1. In the Captures window, right-click the file and select Rename.
  2. In the dialog, specify the name of the file and click OK.

Locating a heap dump file on disk

You can quickly discover where Android Studio stored HPROF files on disk.

To locate an HPROF file on disk:

  • In the Captures window, right-click a heap snapshot file and select Show or Reveal.
  • Android Studio opens an operating system file browser displaying the location where the file resides.

Note: If you move an HPROF file, Android Studio no longer displays it in the Captures window. To display it, use File > Open. Also, if you want to rename the file, do it from the Captures window and not in the operating system file browser.

Deleting a heap dump file

To delete a heap dump file:

  • In the Captures window, right-click a heap snapshot file and select Delete.
  • Android Studio deletes the file from the Captures dialog and from disk.

Converting a heap dump file to standard HPROF format

You can convert an HPROF file to standard format so you can use it outside of Android Studio with other analysis tools.

To convert an HPROF file:

  1. In the Captures window, right-click a heap snapshot file and select Export to standard .hprof.
  2. In the Convert Android Java Heap Dump dialog, specify a filename and click OK.
  3. Android Studio creates a binary HPROF file in the location you specified.