Memory Leak Detection with Heap Snapshots, Allocation Timeline, and Profiling
How to find JavaScript memory leaks using Chrome DevTools heap snapshots, allocation timeline, agent workflows, and Cloudflare Worker memory profiling.
This page summarizes how Chrome DevTools and Chrome DevTools for agents document memory leak detection in browser JavaScript (including WebAssembly), and how Cloudflare Workers document local and production memory profiling for V8 isolates with a 128 MB memory limit per isolate.
Memory leaks, bloat, and garbage collection: what users notice
Section titled “Memory leaks, bloat, and garbage collection: what users notice”Memory problems matter because users often feel them before you see them in metrics. Fix memory problems groups symptoms into three patterns:
A page’s performance gets progressively worse over time. This is possibly a symptom of a memory leak. A memory leak is when a bug in the page causes the page to progressively use more and more memory over time.
— Google, Fix memory problems
Progressive growth usually means reachable objects are not released. Steady slowness may be memory bloat (more memory than the experience needs). Frequent pauses may come from frequent garbage collections, because script execution stops while the browser reclaims memory.
There is no universal byte threshold for “too much” memory; device capabilities differ. The practical approach is to test on hardware your users actually use and treat bad experience on those devices as a signal to profile.
Common memory leak patterns in client-side code
Section titled “Common memory leak patterns in client-side code”Whether you profile manually or with an agent, analysis targets the same retention mistakes. Analyze memory leaks with AI agents lists typical patterns:
- Uncleared event listeners on globals such as
windowordocument - Detached DOM nodes removed from the tree but still held in JavaScript
- Unintentional global variables that never go out of scope
- Closures that keep large objects alive
- Unbounded caches (arrays or maps that grow without limits)
Single-page apps often leak when listeners survive route changes; modals and dropdowns leak when variables keep references to removed nodes; infinite scroll leaks when caches never cap growth.
Memory panel: heap snapshot, allocation timeline, and related profiles
Section titled “Memory panel: heap snapshot, allocation timeline, and related profiles”The Memory panel overview is the main entry for diagnosing JavaScript heap and DOM-related retention. It offers four profile types:
| Profile type | What it shows | Typical use |
|---|---|---|
| Heap snapshot | Distribution among JS objects and DOM nodes at one instant | Find detached nodes, compare before/after |
| Allocation instrumentation on timeline | Allocations over time; objects still live at end of recording | Isolate leaks tied to an action |
| Allocation sampling | Approximate allocations by stack, low overhead | Long-running sessions |
| Detached elements | HTML nodes retained only by JS references | Fast DOM leak checks |
Open the panel from the command menu (Control+Shift+P / Command+Shift+P, then Show Memory), More panels → Memory, or More tools → Memory, as described in Memory panel overview.
Heap snapshots: how to find detached DOM memory leaks
Section titled “Heap snapshots: how to find detached DOM memory leaks”A DOM node is collectible only when nothing in the live DOM or JavaScript references it. Fix memory problems explains detached nodes: removed from the tree but still referenced from code— a frequent leak source.
To capture a snapshot: Memory → Heap snapshot → Take snapshot. Filter with Detached in the class filter, then use the Retainers pane to see what still holds the node. Record heap snapshots recommends the Comparison view to prove a leak: snapshot before an action, perform and reverse the action, snapshot again, and inspect deltas— an operation that should fully unwind should not leave extra instances behind.
Useful Summary columns (see Memory terminology):
- Shallow size — memory held by the object itself (often large for strings and arrays)
- Retained size — memory freed if this object and its exclusive dependents become unreachable
- Distance — hops from a GC root; outliers at larger distance deserve scrutiny
Constructor filters such as Objects retained by detached nodes speed up DOM leak hunts (Record heap snapshots).
Allocation timeline: objects that stay live after an action
Section titled “Allocation timeline: objects that stay live after an action”The Allocation timeline tool combines heap snapshot detail with timeline tracking:
An Allocation timeline report combines the detailed snapshot information of the heap profiler with the incremental updating and tracking of the Timeline panel.
— Google, How to Use the Allocation Timeline Tool
Record with Allocations on timeline, perform the suspected leak path, then stop. Blue bars mean allocations still live in the final snapshot; gray bars were collected. Zoom a bar to filter constructors for that interval, then follow the Retainers tree to see why an object survived.
Snapshots are taken periodically during recording (as often as every 50 ms) plus one at the end; the @ object id supports comparison across snapshots.
Shallow size and retained size when reading snapshots
Section titled “Shallow size and retained size when reading snapshots”This is the size of memory that is freed once the object itself is deleted along with its dependent objects that were made unreachable from GC roots.
— Google, Memory terminology
Small shallow size does not imply a small leak: one reference can retain a large subgraph. When debugging, clear the DevTools console and avoid active breakpoints before snapshots, because console evaluation can retain objects (Memory terminology).
Task Manager and Performance recordings: monitor memory before deep profiling
Section titled “Task Manager and Performance recordings: monitor memory before deep profiling”Fix memory problems suggests starting with the Chrome Task Manager (Shift+Esc): enable JavaScript memory on the column header. Memory footprint reflects OS memory (including DOM nodes); rising JavaScript Memory live value (in parentheses) suggests growing reachable heap objects.
In the Performance panel, enable Memory, record, and bracket the session with forced GC via Collect garbage (Performance features reference). If JS heap or node counters end higher than after an initial forced collection, treat that as a leak candidate— the pattern shown in Google’s grow() button example in Fix memory problems.
Chrome DevTools for agents: three-snapshot leak analysis
Section titled “Chrome DevTools for agents: three-snapshot leak analysis”Chrome DevTools for agents automates heap snapshot collection and analysis for coding agents fixing client-side leaks in JavaScript and WebAssembly:
Chrome DevTools for agents lets your coding agent analyze and fix client-side memory leaks (including JavaScript and WebAssembly) in your application. By automating the collection and analysis of heap snapshots, your agent can pinpoint objects that are not being properly garbage collected and suggest targeted code refactoring.
— Google, Analyze memory leaks with AI agents
Three-snapshot methodology and agent constraints
Section titled “Three-snapshot methodology and agent constraints”Reliable agent workflows usually need three snapshots: baseline before an action, target after the action, and final after reversing the action. Timers (setTimeout) and animations (requestAnimationFrame) may not need a reversal step but can still increase memory.
Always verify before fixing: confirm a detached node is not an intentional cache before nulling references.
Example agent tasks from the same doc: navigate SPA routes and check for uncleared window listeners; open/close a modal repeatedly and hunt Detached HTMLDivElement; scroll an infinite feed to detect unbounded LRU caches; monitor a canvas app for arrays of WebGL meshes growing every frame.
Cloudflare Workers: DevTools memory snapshots and production heap profiles
Section titled “Cloudflare Workers: DevTools memory snapshots and production heap profiles”Workers run in V8 isolates with a 128 MB memory limit; the Memory usage chart (P50–P999) helps spot upward trends after deploys. When usage climbs, Cloudflare points you to local DevTools snapshots and production heap profiling (Profiling Memory).
With wrangler dev (or Vite + Cloudflare plugin), you get Cloudflare’s Chrome DevTools implementation (Using DevTools):
You can use Chrome DevTools to: … Observe memory usage and debug memory leaks in your code that can cause out-of-memory (OOM) errors
— Cloudflare, Using DevTools
-
Run
wrangler devand press D to open DevTools. -
Open the Memory tab and send production-like traffic (volume, routes, or
--remotebindings when needed) (Profiling Memory). -
Choose Take snapshot and inspect Statistics / type breakdown— for example, growing
(string)entries after repeatedresponseText = responseText + ...on a global (Profiling Memory).
For deployed Workers, Profiling in production supports CPU (default) and Heap profiles. A heap profile samples an allocation stack every 512 kB allocated—it measures allocations, not retained memory, and is not a heap snapshot:
Use a Heap profile to identify code that allocates memory. It collects a stack trace every 512 kB of memory allocated during capture, recording the chain of function calls responsible for each sampled allocation.
— Cloudflare, Profiling in production
Capture requires active traffic during the window (1 000–50 000 ms) on a loaded isolate via dashboard Flamegraph, cf workers versions profile, or the Workers API (Profiling in production).
Error 1102 and memory limit patterns on Workers
Section titled “Error 1102 and memory limit patterns on Workers”Error 1102 ties high memory use to concrete code smells:
- Buffering large request or response bodies
- Large globals or arrays that accumulate per request
- Repeated string or array append without streaming
Mitigations align with the same doc: prefer TransformStream or node:stream, avoid holding full bodies in memory, and avoid unbounded accumulation.
Common mistakes when profiling memory
Section titled “Common mistakes when profiling memory”- Taking one snapshot without a before/after or baseline/target/final sequence, so growth looks ambiguous.
- Ignoring intentional caches (detached nodes or globals that are deliberate).
- Profiling with the DevTools console full of evaluated objects, which skews retention (Memory terminology).
- Confusing allocation profiles (Workers heap profile, allocation timeline blues) with proof that memory is still retained at rest.
- Testing Workers locally with unlike traffic, so production leaks do not reproduce (Profiling Memory).
- Letting agents parse raw
.heapsnapshotfiles instead of tool-mediated analysis.
How to check this on your site
Section titled “How to check this on your site”- Reproduce the slow or growing-memory behavior on a target device or local
wrangler dev, then open the Memory panel or Worker DevTools via D (Profiling Memory). - For browser pages, enable JS memory in Chrome Task Manager or a Performance recording with Memory and forced GC (Fix memory problems).
- Capture heap snapshots or an allocation timeline around the suspect action; for Workers in production, capture a Heap profile under load (Profiling in production) and correlate trends with the Memory usage chart.
- Fix retention (listeners, globals, caches, streaming) and re-run comparison snapshots or metrics after deploy.
- You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about.