Daniele Di Proietto | 483fb59 | 2022-11-09 16:43:50 +0000 | [diff] [blame] | 1 | # Memory: Java heap dumps |
Primiano Tucci | a662485 | 2020-05-21 19:12:50 +0100 | [diff] [blame] | 2 | |
Daniele Di Proietto | 483fb59 | 2022-11-09 16:43:50 +0000 | [diff] [blame] | 3 | NOTE: Capturing Java heap dumps requires Android 11 or higher |
Primiano Tucci | a662485 | 2020-05-21 19:12:50 +0100 | [diff] [blame] | 4 | |
| 5 | See the [Memory Guide](/docs/case-studies/memory.md#java-hprof) for getting |
Daniele Di Proietto | 483fb59 | 2022-11-09 16:43:50 +0000 | [diff] [blame] | 6 | started with Java heap dumps. |
Primiano Tucci | a662485 | 2020-05-21 19:12:50 +0100 | [diff] [blame] | 7 | |
Daniele Di Proietto | 483fb59 | 2022-11-09 16:43:50 +0000 | [diff] [blame] | 8 | Conversely from [Native heap profiles](native-heap-profiler.md), Java heap dumps |
| 9 | report full retention graphs of managed objects but not call-stacks. The |
| 10 | information recorded in a Java heap dump is of the form: _Object X retains |
| 11 | object Y, which is N bytes large, through its class member named Z_. |
| 12 | |
| 13 | Java heap dumps are not to be confused with profiles taken by the |
| 14 | [Java heap sampler](native-heap-profiler.md#java-heap-sampling) |
Primiano Tucci | a662485 | 2020-05-21 19:12:50 +0100 | [diff] [blame] | 15 | |
| 16 | ## UI |
| 17 | |
| 18 | Heap graph dumps are shown as flamegraphs in the UI after clicking on the |
| 19 | diamond in the _"Heap Profile"_ track of a process. Each diamond corresponds to |
| 20 | a heap dump. |
| 21 | |
Daniele Di Proietto | 483fb59 | 2022-11-09 16:43:50 +0000 | [diff] [blame] | 22 |  |
Primiano Tucci | a662485 | 2020-05-21 19:12:50 +0100 | [diff] [blame] | 23 | |
Daniele Di Proietto | 483fb59 | 2022-11-09 16:43:50 +0000 | [diff] [blame] | 24 |  |
Primiano Tucci | a662485 | 2020-05-21 19:12:50 +0100 | [diff] [blame] | 25 | |
Daniele Di Proietto | 37ac064 | 2021-10-21 15:10:19 +0100 | [diff] [blame] | 26 | The native size of certain objects is represented as an extra child node in the |
| 27 | flamegraph, prefixed with "[native]". The extra node counts as an extra object. |
Daniele Di Proietto | 483fb59 | 2022-11-09 16:43:50 +0000 | [diff] [blame] | 28 | This is available only on Android 13 or higher. |
Daniele Di Proietto | 37ac064 | 2021-10-21 15:10:19 +0100 | [diff] [blame] | 29 | |
Primiano Tucci | a662485 | 2020-05-21 19:12:50 +0100 | [diff] [blame] | 30 | ## SQL |
| 31 | |
| 32 | Information 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 Proietto | 37ac064 | 2021-10-21 15:10:19 +0100 | [diff] [blame] | 38 | `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 Tucci | a662485 | 2020-05-21 19:12:50 +0100 | [diff] [blame] | 41 | For instance, to get the bytes used by class name, run the following query. |
| 42 | As-is this query will often return un-actionable information, as most of the |
| 43 | bytes in the Java heap end up being primitive arrays or strings. |
| 44 | |
| 45 | ```sql |
| 46 | select 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 | |
| 60 | We can use `experimental_flamegraph` to normalize the graph into a tree, always |
| 61 | taking the shortest path to the root and get cumulative sizes. |
| 62 | Note that this is **experimental** and the **API is subject to change**. |
| 63 | From this we can see how much memory is being held by each type of object |
| 64 | |
Florian Mayer | d1b2f7b | 2020-09-15 19:00:25 +0100 | [diff] [blame] | 65 | For that, we need to find the timestamp and upid of the graph. |
| 66 | |
| 67 | ```sql |
| 68 | select distinct graph_sample_ts, upid from heap_graph_object |
| 69 | ``` |
| 70 | |
| 71 | graph_sample_ts | upid | |
| 72 | --------------------|--------------------| |
| 73 | 56785646801 | 1 | |
| 74 | |
| 75 | We can then use them to get the flamegraph data. |
| 76 | |
Primiano Tucci | a662485 | 2020-05-21 19:12:50 +0100 | [diff] [blame] | 77 | ```sql |
| 78 | select name, cumulative_size |
Tuchila Octavian | d94d624 | 2021-11-15 10:39:56 +0000 | [diff] [blame] | 79 | from experimental_flamegraph |
| 80 | where ts = 56785646801 |
| 81 | and upid = 1 |
| 82 | and profile_type = 'graph' |
Primiano Tucci | a662485 | 2020-05-21 19:12:50 +0100 | [diff] [blame] | 83 | 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 Proietto | 483fb59 | 2022-11-09 16:43:50 +0000 | [diff] [blame] | 98 | The Java heap dump data source is configured through the |
Primiano Tucci | a662485 | 2020-05-21 19:12:50 +0100 | [diff] [blame] | 99 | [JavaHprofConfig](/docs/reference/trace-config-proto.autogen#JavaHprofConfig) |
| 100 | section of the trace config. |
| 101 | |
| 102 | ```protobuf |
| 103 | data_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 | ``` |