blob: b96da6f440f09e7c62e8a0dc003847965b40a8b5 [file] [log] [blame] [view]
Albert J. Wong34b06ef2018-01-11 18:57:261# Description of Tools for developers trying to understand memory usage
2
3This page provides an overview of the tools available for examining memory usage
4in chrome.
5
Albert J. Wong2db98ee22018-01-16 23:17:076## Which tool should I use?
Albert J. Wong34b06ef2018-01-11 18:57:267
8No single tool can give a full view of memory usage in Chrome. There are too
9many different context involved (JS heap, DOM objects, native allocations, GPU,
10etc) that any tool that collected all that information likely would not be able
Albert J. Wong2db98ee22018-01-16 23:17:0711to provide an actionable analysis.
12
13Here is a table of common area of inquiry and suggested tools for examining them.
Albert J. Wong34b06ef2018-01-11 18:57:2614
Albert J. Wong174174762018-01-18 23:50:2315| Topic/Area of Inquiry | Tool(s) |
16|----------------------- | ------- |
17| Which subsystems consuming memory per process. | [Global Memory Dumps](#global-memory-dumps), [Taking memory-infra trace](#memory-infra-trace) |
18| Tracking C++ object allocation over time | [`diff_heap_profiler.py`](#diff-heap-profiler), [Heap Details in chrome://tracing](#heap-dumps-chrome-tracing) |
Kevin Babbittdfa8c8c2023-11-16 16:41:4019| Suspected DOM leaks in the Renderer | [Developer Tools Heap Snapshots](#dev-tools-heap-snapshots), [Real World Leak Detector](#real-world-leak-detector) |
Albert J. Wong174174762018-01-18 23:50:2320| Kernel/Driver Memory and Resource Usage | [perfmon (win), ETW](#os-tools) |
Albert J. Wong2db98ee22018-01-16 23:17:0721| Blackbox examination of process memory | [VMMAP (win)](#os-tools) | Understanding fragmentation of the memory space |
Albert J. Wong174174762018-01-18 23:50:2322| Symbolized Heap Dump data | [Heap Dumps](#heap-dumps) | Grabs raw data for analysis by other tools |
Albert J. Wong2db98ee22018-01-16 23:17:0723
24If that seems like a lot of tools and complexity, it is [but there's a reason](#no-one-true-metric).
25
Albert J. Wong174174762018-01-18 23:50:2326-----------
27## <a name="global-memory-dumps"> Global Memory Dumps
Albert J. Wong39485112018-01-17 23:07:5128Many Chrome subsystems implement the
Amos Limf916d572018-05-21 23:10:3529[`trace_event::MemoryDumpProvider`](../../base/trace_event/memory_dump_provider.h)
Albert J. Wong39485112018-01-17 23:07:5130interface to provide self-reported stats detailing their memory usage. The
Albert J. Wong174174762018-01-18 23:50:2331Global Memory Dump view provides a snapshot-oriented view of these subsystems
32that can be collected and viewed via the chrome://tracing infrastructure.
Albert J. Wong39485112018-01-17 23:07:5133
34In the Analysis split screen, a single roll-up number is provided for each of
35these subsystems. This can give a quick feel for where memory is allocated. The
36cells can then be clicked to drill into a more detailed view of the subsystem's
37stats. The memory-infra docs have more [detailed descriptions for each column](../memory-infra#Columns).
38
39To look a the delta between two dumps, control-click two different dark-purple M
40circles.
41
42### Blindspots
Quinten Yearsley317532d2021-10-20 17:10:3143 * Statistics are self-reported. If the MemoryDumpProvider implementation does
Albert J. Wong39485112018-01-17 23:07:5144 not fully cover the resource usage of the subsystem, those resources will
45 not be accounted.
46
47### Instructions
48 1. Take a memory-infra trace
49 2. Click on a *dark-purple* M circle. Each one of these corresponds to a heavy
50 dump.
51 3. Click on a (process, subsystem) cell in `Global Memory Dump` tab within the
52 Analysis View in bottom split screen.
53 4. *Scroll down* to the bottom of the lower split screen to see details of
54 selection (process, subsystem)
55
56Clicking on the cell pulls up a view that lets you examine the stats
57collected by the given MemoryDumpProvider however that view is often way outside
58the viewport of the analysis view. Be sure to scroll down.
59
60
Albert J. Wong174174762018-01-18 23:50:2361-----------
62## <a name="heap-dumps-chrome-tracing"> Heap Dumps in chrome://tracing
63GUI method of exploring the heap dump for a process.
Albert J. Wong39485112018-01-17 23:07:5164
Albert J. Wong174174762018-01-18 23:50:2365TODO(awong): Explain how to interpret + interact with the data. (e.g. threads,
66bottom-up vs top-down, etc)
Albert J. Wong39485112018-01-17 23:07:5167
68### Blindspots
Albert J. Wong174174762018-01-18 23:50:2369 * As this is a viewer of [heap dump](#heap-dump) data, it has the same
70 blindspots.
71 * The tool is bound by the memory limits of chrome://tracing. Large dumps
72 (which generate large JS strings) will not be loadable and may likely crash
73 chrome://tracing.
Albert J. Wong39485112018-01-17 23:07:5174
75### Instructions
Albert J. Wong174174762018-01-18 23:50:2376 1. [Configure Out-of-process heap profiling](#configure-oophp)
Albert J. Wong39485112018-01-17 23:07:5177 2. Take a memory-infra trace and symbolize it.
78 3. Click on a *dark-purple* M circle.
79 4. Find the cell corresponding to the allocator (list below) for the process of interest within the `Global Memory Dump` tab of the Analysis View.
80 5. Click on "hotdog" menu icon next to the number. If no icon is shown, the
81 trace does not contain a heap dump for that allocator.
82 6. *Scroll down* to the bottom of the lower split screen. There should now
83 be a "Heap details" section below the "Component details" section that
84 shows a all heap allocations in a navigatable format.
85
86On step 5, the `Component Details` and `Heap Dump` views that let you examine
87the information collected by the given MemoryDumpProvider is often way outside
88the current viewport of the Analysis View. Be sure to scroll down!
89
Albert J. Wong39485112018-01-17 23:07:5190Currently supported allocators: malloc, PartitionAlloc, Oilpan.
91
92Note: PartitionAlloc and Oilpan traces have unsymbolized Javascript frames
93which often make exploration via this tool hard to consume.
94
Albert J. Wong2db98ee22018-01-16 23:17:0795
Albert J. Wong174174762018-01-18 23:50:2396-----------
97
98## <a name="diff-heap-profiler"></a> `diff_heap_profiler.py`
99This is most useful for examining allocations that occur during an interval of
100time. This is often useful for finding leaks as one call-stack will rise to the
101top as the leak is repeated triggered.
102
103Multiple traces can be given at once to show incremental changes. A similar
104analysis can be had via ctrl-clicking multiple Global Memory Dumps in the
105chrome://tracing UI but loading multiiple detailed heapdumps can often crash the
106chrome://tracing UI. This tool is more robust to large data sizes.
107
108The source code can also be used as an example for manually processing heap dump
109data in python.
110
111TODO(awong): Write about options to script and the flame graph.
112
113### Blindspots
114 * As this is a viewer of [heap dump](#heap-dumps) data, it has the same
115 blindspots.
116
117### Instructions
118 1. Get 2 or more [symbolized heap dump](#heap-dumps)
John Palmer046f9872021-05-24 01:24:56119 3. Run resulting traces through [`diff_heap_profiler.py`](https://chromium.googlesource.com/catapult/+/main/experimental/tracing/bin/diff_heap_profiler.py) to show a list of new allocations.
Albert J. Wong174174762018-01-18 23:50:23120
121-----------
122## <a name="heap-dumps"></a>Heap Dumps
123Heap dumps provide extremely detailed data about object allocations and is
124useful for finding code locations that are generating a large number of live
125allocations. Data is tracked and recorded using the [Out-of-process Heap
Amos Limf916d572018-05-21 23:10:35126Profiler (OOPHP)](../../components/services/heap_profiling/README.md).
Albert J. Wong174174762018-01-18 23:50:23127
128For the Browser and GPU process, this often quickly finds objects that leak over
129time.
130
131This is less useful in the Renderer process. Even though Oilpan and
132PartitionAlloc are hooked into the data collection, many of the stacks end up
133looking similar due to the nature of DOM node allocation.
134
135### Blindspots
136 * Heap dumps only catch allocations that pass through the allocator shim. In particular,
137 calls made directly to the platform's VM subsystem (eg, via `mmap()` or
138 `VirtualAlloc()`) will not be tracked.
139 * Utility processes are currently not profiled.
140 * Allocations are only recorded after the
Amos Limf916d572018-05-21 23:10:35141 [HeapProfilingService](../../components/services/heap_profiling/heap_profiling_service.h)
erikchenfa983faa2018-04-05 18:56:42142 has spun up the profiling process and created a connection to the target
143 process. The HeapProfilingService is a mojo service that can be configured to
144 start early in browser startup but it still takes time to spin up and early
145 allocations are thus lost.
Albert J. Wong174174762018-01-18 23:50:23146
147### Instructions
148#### <a name="configure-oophp"></a>Configuration and setup
149 1. [Android Only] For native stack traces, a custom build with
Darwin Huang6ba47562019-12-18 21:28:59150 `enable_framepointers=true` is required.
Albert J. Wong174174762018-01-18 23:50:23151 2. Configure OOPHP settings in about://flags. (See table below)
152 3. Restart browser with new settings if necessary.
153 4. Verify target processes are being profiled in chrome://memory-internals.
154 5. [Optional] start profiling additional processes in chrome://memory-internals.
155
156| Flag | Notes |
157| ------- | ----- |
158| Out of process heap profiling start mode. | This option is somewhat misnamed. It tells OOPHP which processes to profile at startup. Other processes can selected manually later via chrome://memory-internals even if this is set to "disabled". |
159| Keep track of even the small allocations in memlog heap dumps. | By default, small allocations are not emitted in the heap dump to reduce dump size. Enabling this track _all_ allocations. |
Quinten Yearsley317532d2021-10-20 17:10:31160| The type of stack to record for memlog heap dumps | If possible, use Native stack frames as that provides the best information. When those are not available either due to performance for build (eg, no frame-pointers on arm32 official) configurations, using trace events for a "pseudo stack" can give good information too. |
Albert J. Wong174174762018-01-18 23:50:23161| Heap profiling | Deprecated. Enables the in-process heap profiler. Functionality should be fully subsumed by preceeding options. |
162
163#### Saving a heap dump
164 1. On Desktop, click "save dump" in chrome://memory-internals to save a
165 dump of all the profiled processes. On Android, enable debugging via USB
166 and use chrome://inspect/?tracing#devices to take a memory-infra trace
167 which will have the heap dump embedded.
Erik Chenfdf9afc2018-01-23 20:19:35168 2. Symbolize trace using [`symbolize_trace.py`](../../third_party/catapult/tracing/bin/symbolize_trace). If the Chrome binary was built locally, pass the flag "--is-local-build".
Albert J. Wong174174762018-01-18 23:50:23169 3. Analyze resuing heap dump using [`diff_heap_profiler.py`](#diff-heap-profiler), or [Heap Profile view in Chrome Tracing](#tracing-heap-profile)
170
Quinten Yearsley317532d2021-10-20 17:10:31171On desktop, using chrome://memory-internals to take a heap dump is more reliable
Albert J. Wong174174762018-01-18 23:50:23172as it directly saves the heapdump to a file instead of passing the serialized data
173through the chrome://tracing renderer process which can easily OOM. For Android,
174this native file saving was harder to implement and would still leave the
175problem of getting the dump off the phone so memory-infra tracing is the
176current recommended path.
177
178-----------
179## <a name="memory-infra-trace"></a> Taking a memory-infra trace.
180Examining self-reported statistics from various subsystems on memory usages.
181This is most useful for getting a high-level understanding of how memory is
182distributed between the different heaps and subsystems in chrome.
183
184It also provides a way to view heap dump allocation information collected per
185process through a progressively expanding stack trace.
186
187Though chrome://tracing itself is a timeline based plot, this data is snapshot
188oriented. Thus the standard chrome://tracing plotting tools do not provide a
189good means for measuring changes per snapshot.
190
191### Blindspots
192 * Statistics are self-reported via "Memory Dump Provider" interfaces. If there
193 is an error in the data collection, or if there are privileged resources
194 that cannot be easily measured from usermode, they will be missed.
195
196### Instructions
197 1. Visit chrome://tracing
198 2. Start a trace for memory-infra
199 1. Click the "Record" button
200 2. Choose "Manually select settings"
201 3. [optional] Clear out all other tracing categories.
202 4. Select "memory-infra" from the "Disabled by Default Categories"
203 5. Click record again.
204 3. Wait for a few seconds for a Global Memory Dump to be taken. If OOPHP
205 is enabled, don't run for more than a few seconds to avoid crashing the
206 chrome://tracing UI with an over-large trace.
207 4. Wait for a few seconds for a Global Memory Dump to be taken.
208 5. Click stop
209
210This should produce a view of the trace file with periodic "light" and "heavy"
211memory dumps. These dumps are created periodically so the time spent waiting
212in step (3) determines how many dumps (which are snapshots) are taken.
213
214**Warning:** If OOPHP is enabled, the tracing UI may not be able to handle