Skip to content

Memory leak when opening and closing windows #21586

Closed

Description

@t57ser

Issue Details

  • Electron Version:
    • 5, 6, 7, 8
  • Operating System:
    • Windows 10
  • Last Known Working Electron version:
    • ?

Expected Behavior

Opening and closing window should not increase memory

Actual Behavior

Memory will increase for each window even after the window has been destroyed

To Reproduce

const { app, BrowserWindow, BrowserView } = require("electron");

const init = () => {
	let browserWindow2 = new BrowserWindow({
		show: true,
	});
	let j = 0;
	const interval = setInterval(() => {
		j++;
		if (j > 10) {
			clearInterval(interval);
		}
		for(let i = 0; i < 20; i++) {
			let browserWindow = new BrowserWindow({
				show: true,
			});
			setTimeout(() => {
				browserWindow.close();
			}, 5000)
		}
	}, 1000);

};
app.on("ready", () => {
	init();
});

The above code snipped should not result in more memory than just opening a single window.
I also tried to call the garbage collector manually and I also tried browserWindow.destroy(). No difference.

Activity

  1. jackple commented on Jul 16, 2020

    @jackple

    Is there any solution?

  2. pushkin- commented on Dec 10, 2020

    @pushkin-

    Can someone test if this repros in Electron 11?

  3. t57ser commented on Dec 10, 2020

    @t57ser
    ContributorAuthor

    Still happening in electron 11. Opening a single window results in 40mb used RAM while executing the code i posted results in about double the memory.

  4. VladTabaka commented on Dec 11, 2020

    @VladTabaka

    Same here (versions 9 and 11) is it somehow related to #23578 ?

  5. pushkin- commented on Dec 11, 2020

    @pushkin-

    @VladTabaka That's more related to BrowserViews and the PR wouldn't be in the older Electron releases that are affected.

  6. nornagon commented on Dec 16, 2020

    @nornagon
    Contributor

    Did some experimenting with LSan and this looks like a possible candidate:

    =================================================================
    ==43956==ERROR: LeakSanitizer: detected memory leaks
    
    Direct leak of 32640 byte(s) in 40 object(s) allocated from:
        #0 0x100593330 in __asan_memmove (/Users/nornagon/work/electron-gn/src/out/Testing-asan/Electron.app/Contents/MacOS/libclang_rt.asan_osx_dynamic.dylib:x86_64+0x43330)
        #1 0x112b5d1e7 in operator new(unsigned long) (/Users/nornagon/work/electron-gn/src/out/Testing-asan/Electron.app/Contents/Frameworks/Electron Framework.framework/Versio
    ns/A/Electron Framework:x86_64+0x1128d1e7)
        #2 0x101fb9954 in electron::NativeWindowMac::NativeWindowMac(gin_helper::Dictionary const&, electron::NativeWindow*) (/Users/nornagon/work/electron-gn/src/out/Testing-as
    an/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework:x86_64+0x6e9954)
        #3 0x101fcda7a in electron::NativeWindow::Create(gin_helper::Dictionary const&, electron::NativeWindow*) (/Users/nornagon/work/electron-gn/src/out/Testing-asan/Electron.
    app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework:x86_64+0x6fda7a)
        #4 0x101993286 in electron::api::BaseWindow::BaseWindow(v8::Isolate*, gin_helper::Dictionary const&) (/Users/nornagon/work/electron-gn/src/out/Testing-asan/Electron.app/
    Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework:x86_64+0xc3286)
        #5 0x1019dd3df in electron::api::BrowserWindow::BrowserWindow(gin::Arguments*, gin_helper::Dictionary const&) (/Users/nornagon/work/electron-gn/src/out/Testing-asan/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework:x86_64+0x10d3df)
        #6 0x1019e5011 in electron::api::BrowserWindow::New(gin_helper::ErrorThrower, gin::Arguments*) (/Users/nornagon/work/electron-gn/src/out/Testing-asan/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework:x86_64+0x115011)
    [...]
    

    not 100% sure about my methodology, but I thought I'd post it here anyway :)

  7. nornagon commented on Feb 4, 2021

    @nornagon
    Contributor

    This is an odd one. Here are some results from my experimentation.

    experiment code
    const { app, BrowserWindow, BaseWindow } = require("electron");
    
    const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms))
    
    const init = async () => {
      const n = 200
      const warmup = 5
      const numWindows = 200
      let increase = 0
    
      let lastRss
      for (let j = -warmup; j < n; j++) {
        const windows = []
        for(let i = 0; i < numWindows; i++) {
          windows.push(new BaseWindow({ show: false }));
        }
        for (const w of windows) {
          w.close();
        }
        gc()
        await wait(10)
        const rss = process.memoryUsage().rss
        if (lastRss)
          console.log(
    	`rss=${rss / 1e6} MB (${rss - lastRss >= 0 ? '+' : ''}${(rss - lastRss) / 1e6} MB)`
          );
        if (j >= 0)
          increase += rss - lastRss
        lastRss = rss
      }
      console.log(`average increase: ${increase / n / 1e6} MB`)
      app.quit()
    };
    
    app.whenReady().then(init)
    
    app.on('window-all-closed', () => {})

    Results:

    • leak still present when instantiating BaseWindow instead of BrowserWindow
    • not every iteration creates leaks. Sometimes the rss after creating windows is the same as before, or even lower than it was before!
    • creating 10x as many windows each iteration increases the leaked amount by less than 10x
    • v8 heap size doesn't increase, so the leak is off-heap.
    • avg increase per iteration with n=2000, numWindows=1 is 0.004796416 MB. with n=2000, numWindows=0 (no windows created!!), the average increase per iteration is nearly the same, at 0.004251648 MB. at numWindows=10, the value is 3x bigger, at 0.015570944 MB.
    • i tried replacing 'create windows' with 'wait about as long as it takes to create those windows' to rule out the possibility that it was some idle behavior causing the leak. it doesn't seem to be.
  8. nornagon commented on Feb 4, 2021

    @nornagon
    Contributor

    This is at least one source of leakage:

    electron::ElectronBrowserMainParts::Get()->RegisterDestructionCallback(
    GetDestroyClosure());

    Every time a window is created, we register a callback to destroy it at shutdown, but we never clear those callbacks.

  9. hovancik commented on Apr 21, 2021

    @hovancik

    Still and issue on electron 12. Same on Linux. Not sure about mac.

  10. axetroy commented on Oct 13, 2021

    @axetroy

    confirm on windows with Electron 11.x.

    But macOS works fine.

  11. nornagon commented on Mar 10, 2022

    @nornagon
    Contributor
  12. github-actions commented on Oct 8, 2022

    @github-actions
    Contributor

    This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

  13. pushkin- commented on Oct 10, 2022

    @pushkin-

    I do see some memory being reclaimed on Windows 10 Electron 21, but maybe not all of it? Would be good to get some eyes on this.

  14. 44 remaining items

  15. electron-issue-triage commented on Dec 23, 2025

    @electron-issue-triage

    This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

  16. gpetrov commented on Dec 23, 2025

    @gpetrov

    Bump

  17. electron-issue-triage commented on Mar 24, 2026

    @electron-issue-triage

    This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

  18. gpetrov commented on Mar 24, 2026

    @gpetrov

    Bump

  19. electron-issue-triage commented on Jun 23, 2026

    @electron-issue-triage

    This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

  20. pushkin- commented on Jun 23, 2026

    @pushkin-

    Bump

  21. electron-issue-triage commented on Sep 23, 2026

    @electron-issue-triage

    This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions