blob: e3d5921054a74cb2229b95684500e20fce5da6f6 [file] [log] [blame] [view]
Daniele Di Proietto483fb592022-11-09 16:43:50 +00001# Memory: Java heap dumps
Primiano Tuccia6624852020-05-21 19:12:50 +01002
Daniele Di Proietto483fb592022-11-09 16:43:50 +00003NOTE: Capturing Java heap dumps requires Android 11 or higher
Primiano Tuccia6624852020-05-21 19:12:50 +01004
5See the [Memory Guide](/docs/case-studies/memory.md#java-hprof) for getting
Daniele Di Proietto483fb592022-11-09 16:43:50 +00006started with Java heap dumps.
Primiano Tuccia6624852020-05-21 19:12:50 +01007
Daniele Di Proietto483fb592022-11-09 16:43:50 +00008Conversely from [Native heap profiles](native-heap-profiler.md), Java heap dumps
9report full retention graphs of managed objects but not call-stacks. The
10information recorded in a Java heap dump is of the form: _Object X retains
11object Y, which is N bytes large, through its class member named Z_.
12
13Java heap dumps are not to be confused with profiles taken by the
14[Java heap sampler](native-heap-profiler.md#java-heap-sampling)
Primiano Tuccia6624852020-05-21 19:12:50 +010015
16## UI
17
18Heap graph dumps are shown as flamegraphs in the UI after clicking on the
19diamond in the _"Heap Profile"_ track of a process. Each diamond corresponds to
20a heap dump.
21
Daniele Di Proietto483fb592022-11-09 16:43:50 +000022![Java heap dumps in the process tracks](/docs/images/profile-diamond.png)
Primiano Tuccia6624852020-05-21 19:12:50 +010023
Daniele Di Proietto483fb592022-11-09 16:43:50 +000024![Flamegraph of a Java heap dump](/docs/images/java-heap-graph.png)
Primiano Tuccia6624852020-05-21 19:12:50 +010025
Daniele Di Proietto37ac0642021-10-21 15:10:19 +010026The native size of certain objects is represented as an extra child node in the
27flamegraph, prefixed with "[native]". The extra node counts as an extra object.
Daniele Di Proietto483fb592022-11-09 16:43:50 +000028This is available only on Android 13 or higher.
Daniele Di Proietto37ac0642021-10-21 15:10:19 +010029
Primiano Tuccia6624852020-05-21 19:12:50 +010030## SQL
31
32Information about the Java Heap is written to the following tables:
33
34* [`heap_graph_class`](/docs/analysis/sql-tables.autogen#heap_graph_class)
35* [`heap_graph_object`](/docs/analysis/sql-tables.autogen#heap_graph_object)
36* [`heap_graph_reference`](/docs/analysis/sql-tables.autogen#heap_graph_reference)
37
Daniele Di Proietto37ac0642021-10-21 15:10:19 +010038`native_size` (available only on Android T+) is extracted from the related
39`libcore.util.NativeAllocationRegistry` and is not included in `self_size`.
40
Primiano Tuccia6624852020-05-21 19:12:50 +010041For instance, to get the bytes used by class name, run the following query.
42As-is this query will often return un-actionable information, as most of the
43bytes in the Java heap end up being primitive arrays or strings.
44
45```sql
46select c.name, sum(o.self_size)
47 from heap_graph_object o join heap_graph_class c on (o.type_id = c.id)
48 where reachable = 1 group by 1 order by 2 desc;
49```
50
51|name |sum(o.self_size) |
52|--------------------|--------------------|
53|java.lang.String | 2770504|
54|long[] | 1500048|
55|int[] | 1181164|
56|java.lang.Object[] | 624812|
57|char[] | 357720|
58|byte[] | 350423|
59
60We can use `experimental_flamegraph` to normalize the graph into a tree, always
61taking the shortest path to the root and get cumulative sizes.
62Note that this is **experimental** and the **API is subject to change**.
63From this we can see how much memory is being held by each type of object
64
Florian Mayerd1b2f7b2020-09-15 19:00:25 +010065For that, we need to find the timestamp and upid of the graph.
66
67```sql
68select distinct graph_sample_ts, upid from heap_graph_object
69```
70
71graph_sample_ts | upid |
72--------------------|--------------------|
73 56785646801 | 1 |
74
75We can then use them to get the flamegraph data.
76
Primiano Tuccia6624852020-05-21 19:12:50 +010077```sql
78select name, cumulative_size
Tuchila Octaviand94d6242021-11-15 10:39:56 +000079 from experimental_flamegraph
80 where ts = 56785646801
81 and upid = 1
82 and profile_type = 'graph'
Primiano Tuccia6624852020-05-21 19:12:50 +010083 order by 2 desc;
84```
85
86| name | cumulative_size |
87|------|-----------------|
88|java.lang.String|1431688|
89|java.lang.Class<android.icu.text.Transliterator>|1120227|
90|android.icu.text.TransliteratorRegistry|1119600|
91|com.android.systemui.statusbar.phone.StatusBarNotificationPresenter$2|1086209|
92|com.android.systemui.statusbar.phone.StatusBarNotificationPresenter|1085593|
93|java.util.Collections$SynchronizedMap|1063376|
94|java.util.HashMap|1063292|
95
96## TraceConfig
97
Daniele Di Proietto483fb592022-11-09 16:43:50 +000098The Java heap dump data source is configured through the
Primiano Tuccia6624852020-05-21 19:12:50 +010099[JavaHprofConfig](/docs/reference/trace-config-proto.autogen#JavaHprofConfig)
100section of the trace config.
101
102```protobuf
103data_sources {
104 config {
105 name: "android.java_hprof"
106 java_hprof_config {
107 process_cmdline: "com.google.android.inputmethod.latin"
108 dump_smaps: true
109 }
110 }
111}
112```