background

To conclude, this article focuses on the hprof file structure and parsing hprof files using the Shark library.

Memory profilers in Android Studio

AS first parses the hprof file to obtain the corresponding data information, and then carries out the corresponding visual display and corresponding source code implementation, which will not be detailed here.

After viewing a Memory snapshot file in Android Studio, we can see the following information.

In the classes list, you can see the following information:

  • Heap Count: the number of instances in the Heap.
  • Shallow Size: The total Size of all instances in the heap.
  • Retained Size: The total memory Retained by all instances of this class.

At the top of the class list, you can use the left drop-down list to switch between the following heap dumps:

  • App heap: The main heap where an application allocates memory.
  • Image Heap: A system boot Image that contains classes preloaded during boot. The allocation here is guaranteed never to move or leave.
  • Zygote heap: copy heap at write time for distribution application processes in Android.

By default, lists are sorted by reserved size columns. You can click on any column header to change how the list is sorted. In Instance View, each Instance includes the following:

  • Depth: Shortest number of hops from any GC root to the selected instance.
  • Shallow Size: The Size of this instance.
  • Retained Size: The Size of memory Retained by this instance (based on the dominator tree).

Hprof file format

1. Hprof is a binary file generated by the JVM TI Agent Hprof. The specific file format can be seen in the official document:

As shown in the figure above, Hprof file is composed of file input and file content. The file content is composed of a series of records, the types of which are distinguished by TAG.

Hprof file read

The overall process: first read the file header, then read the record, distinguish the type of record according to the TAG, and then read various information according to the format given by the HPROF Agent.

Corresponding data structure definition

  • Header file: HprofHeader, which records the meta information of the hprof file.

  • File contents: HprofRecord represents an Hprof record, for example, StringRecord represents a string, ClassDumpRecord represents memory class and interface information, and InstanceDumpRecord represents memory instance information.

Header file parsing

Call HprofHeader#parseHeaderOf and parse according to the above rules to get the corresponding header information.

Record content parsing

StreamingHprofReader

Start reading at the end of the initio file.

Open the while loop, continuously reading and retrieving the contents of each record.

According to the corresponding TAG, read the Body content, create the corresponding Record object for saving, convenient later query.

HprofHeapGraph

The HprofHeapGraph will save all the records that are read from above. HprofHeapGraph can be used to find the corresponding memory data based on the criteria.

For example, you can find all the bitmaps in memory by making a call like this.

RetainedSize calculation

ShallowSize: The size of this instance. RetainedSize: this example of memory Size (calculated according to the dominant tree) Shallow Size and Retained the understanding of the Size: www.jianshu.com/p/851b5bb0a…

BuildDominatorTree: ObjectDominators, call buildDominatorTree to build a DominatorTree. The return value is a Map<Long, DominatorNode>, the key is each instance’s objectId, and the value is the corresponding tree node DominatorNode. This will get the shallowSize and retainedSize of an instance.

Memory leak analysis

HeapAnalyzer, which calls the Analyze method to automatically perform memory leak analysis, build GC reference chains, and find leak paths.

Rules for determining leakage:

AndroidObjectInspectors defines a set of common Android rules for checking whether an object leaks and leaks, including VIEW, EDITOR, ACTIVITY, CONTEXT_WRAPPER, DIALOG, etc.

Such as finding destroyed activities or fragments

Related articles

Shallow Size and Retained Size Hprof file Leakcanary – Hprof shark source code analysis

conclusion

So much for the Hprof file and the Shark library. The next article will cover some custom analytics and writing visual pages using Desktop Compose.