--- url: "https://web.dev/articles/detached-window-memory-leaks" titulek: "Detached window memory leaks | Articles | web.dev" h1: "Detached window memory leaks Stay organized with collections Save and categorize content based on your preferences." popis: "Detached windows are a common type of memory leak that is particularly difficult to find and fix." jazyk: "en" kanonicka: "https://web.dev/articles/detached-window-memory-leaks" drobecky: ["Articles", "Detached window memory leaks"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 01:48:12" znaku: 20800 slov: 3099 kodovani: "iso-8859-2" sha256: "e72a415b9822a6d3bf7b2556d128d4d11ca2af19c202a30690c8596ba34af16f" --- Detached window memory leaks Stay organized with collections Save and categorize content based on your preferences. Find and fix tricky memory leaks caused by detached windows. Bartek Nowierski Jason Miller What's a memory leak in JavaScript? A memory leak is an unintentional increase in the amount of memory used by an application over time. In JavaScript, memory leaks happen when objects are no longer needed, but are still referenced by functions or other objects. These references prevent the unneeded objects from being reclaimed by the garbage collector. The job of the garbage collector is to identify and reclaim objects that are no longer reachable from the application. This works even when objects reference themselves, or cyclically reference each other–once there are no remaining references through which an application could access a group of objects, it can be garbage collected. `let A = {}; console.log(A); // local variable reference let B = {A}; // B.A is a second reference to A A = null; // unset local variable reference console.log(B.A); // A can still be referenced by B B.A = null; // unset B's reference to A // No references to A are left. It can be garbage collected.` A particularly tricky class of memory leak occurs when an application references objects that have their own lifecycle, like DOM elements or popup windows. It's possible for these types of objects to become unused without the application knowing, which means application code may have the only remaining references to an object that could otherwise be garbage collected. What's a detached window? In the following example, a slideshow viewer application includes buttons to open and close a presenter notes popup. Imagine a user clicks **Show Notes**, then closes the popup window directly instead of clicking the **Hide Notes** button–the `notesWindow` variable still holds a reference to the popup that could be accessed, even though the popup is no longer in use. ` ` This is an example of a detached window. The popup window was closed, but our code has a reference to it that prevents the browser from being able to destroy it and reclaim that memory. When a page calls `window.open()` to create a new browser window or tab, a [`Window`](https://developer.mozilla.org/docs/Web/API/Window) object is returned that represents the window or tab. Even after such a window has been closed or the user has navigated it away, the `Window` object returned from `window.open()` can still be used to access information about it. This is one type of detached window: because JavaScript code can still potentially access properties on the closed `Window` object, it must be kept in memory. If the window included a lot of JavaScript objects or iframes, that memory can't be reclaimed until there are no remaining JavaScript references to the window's properties. Using Chrome DevTools to demonstrate how it's possible to retain a document after a window has been closed. The same issue can also occur when using `