Skip to content

Memory leak observed upon closing Flutter windows #138893

Closed

Description

@yaminet1024

Steps to reproduce

  1. Navigate to the demo directory with cd demo_win
  2. flutter pub get
  3. flutter run -d windows --release
  4. Navigate through the application: open the menu, select 'New Flutter Window', then close the Flutter Window.
  5. Repeat step 4 ten times. You will notice a consistent increase in memory usage.

first:
企业微信截图_1ed6067c-5d4e-4892-b6be-2be6ba31b6d6

ten times:
企业微信截图_e56da68b-c91e-473b-b826-0350b7458e25

Code sample

see the flutter example project
demo_win.zip

What target platforms are you seeing this bug on?

Windows

OS/Browser name and version | Device information

Windows 11 x64 13th Gen Intel(R) Core(TM) i7-13700KF 3.42 GHz

Does the problem occur on emulator/simulator as well as on physical devices?

Unknown

Logs

No response

Flutter Doctor output

[✓] Flutter (Channel stable, 3.16.0, on macOS 13.6.1 22G313 darwin-x64, locale zh-Hans-CN)
[!] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
✗ cmdline-tools component is missing
Run path/to/sdkmanager --install "cmdline-tools;latest"
See https://developer.android.com/studio/command-line for more details.
✗ Android license status unknown.
Run flutter doctor --android-licenses to accept the SDK licenses.
See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.
[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.3)
[✓] VS Code (version 1.84.2)
[✓] Connected device (2 available)
[✓] Network resources

Activity

  1. added
    in triagePresently being triaged by the triage team
    on Nov 23, 2023
  2. huycozy commented on Nov 23, 2023

    @huycozy
    Member

    Hi @yaminet1024
    The multi-window has not been supported yet so I'm unfortunately going to close this issue. Please follow #30701 for further updates. Thanks!

  3. added
    r: invalidIssue is closed as not valid
    and removed
    in triagePresently being triaged by the triage team
    on Nov 23, 2023
  4. yaminet1024 commented on Nov 24, 2023

    @yaminet1024
    Author

    @huycozy hi, I think this is not a issue related to multi-windows. This problem will also occur when a single window is opened and closed. The purpose of repeating it 10 times is to reflect the increase in memory. This is a desktop engine memory leak I think.

  5. huycozy commented on Nov 27, 2023

    @huycozy
    Member

    Re-opening the issue for further investigation.

    I tried running the example on my Windows Microsoft Windows [Version 10.0.19045.3693] but the issue didn't appear there. I observed app memory from Task Manager and noticed it consumed ~50MB every time opening New Flutter Window (from 20MB to 70MB), and then it got back to 20MB after closing New Flutter Window (tried this more than 10 times).

    On your machine, does the memory continue to increase the more times you try?

  6. added
    in triagePresently being triaged by the triage team
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    and removed
    r: invalidIssue is closed as not valid
    on Nov 27, 2023
  7. yaminet1024 commented on Nov 27, 2023

    @yaminet1024
    Author

    Re-opening the issue for further investigation.

    I tried running the example on my Windows Microsoft Windows [Version 10.0.19045.3693] but the issue didn't appear there. I observed app memory from Task Manager and noticed it consumed ~50MB every time opening New Flutter Window (from 20MB to 70MB), and then it got back to 20MB after closing New Flutter Window (tried this more than 10 times).

    On your machine, does the memory continue to increase the more times you try?

    Yes, I am very sure that there is a memory leak problem. As you can see from my screenshots, from 37.2 to 44.1, there will be an average leak of about 1m at a time. In our actual complex project, there will be 3m at a time.

  8. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Nov 27, 2023
  9. 11 remaining items

  10. loic-sharma commented on Dec 1, 2023

    @loic-sharma
    Member

    Thanks for reporting this! The leaks reproduce on my machine and are primarily allocations by ANGLE. However, our shutdown logic appears to be correctly tearing down the EGL contexts and display. I wonder if we're somehow not cleaning up properly, or, if ANGLE itself is leaking.

  11. yaminet1024 commented on Dec 2, 2023

    @yaminet1024
    Author

    Thanks for reporting this! The leaks reproduce on my machine and primarily allocations by ANGLE. However, our shutdown logic appears to be correctly tearing down the EGL contexts and display. I wonder if we're somehow not cleaning up properly, or, if ANGLE itself is leaking.

    I've been analyzing this issue and identified one problem: Flutter creates EGL contexts on the IO or raster thread but releases them on the platform thread. This does not correctly release the ANGLE thread; it needs to destroy the context on the thread where it was created. Even after fixing the ANGLE thread leak, there are still many leaks present. I am actively working to analyze these additional leaks.

  12. loic-sharma commented on Dec 2, 2023

    @loic-sharma
    Member

    @yaminet1024 Hm, on Windows we create the render and resource contexts on the platform thread at start up, before the engine is started and before the IO & raster threads exist. I wasn't aware of this ANGLE requirement though, where can I learn more about this topic? Perhaps our threading strategies is causing these leaks.

  13. yaminet1024 commented on Dec 12, 2023

    @yaminet1024
    Author

    @yaminet1024 Hm, on Windows we create the render and resource contexts on the platform thread at start up, before the engine is started and before the IO & raster threads exist. I wasn't aware of this ANGLE requirement though, where can I learn more about this topic? Perhaps our threading strategies is causing these leaks.

    Hi, Apologies for the delayed response. ,I found the problem just to debug angle code, I've pinpointed the issue to how eglMakeCurrent manages thread data. It appears that when eglMakeCurrent is called on the IO/Raster thread, the context gains a reference count, which is only decremented when eglMakeCurrent is invoked with EGL_NO_CONTEXT on the same thread. However, I couldn't find a call to makeCurrent(,,EGL_NO_CONTEXT) in the AngleManager.

    Here are the relevant sections of the ANGLE codebase that I've been looking into:

    Binding the context to a thread:
    eglMakeCurrent Entry Point
    eglMakeCurrent Stub
    Display.cpp
    I also discovered that eglDestroyContext will not fully clear all data If the context is still current on at least 1 thread.
    eglDestroyContext

    Furthermore, eglTerminate does not clear all the data either.
    eglTerminate

    It seems that flutter threading strategy might not be accounting for this reference count behavior, which could be leading to the leaks we're experiencing.

    If I set EGL_NO_CONTEXT during the clear time on the raster/IO thread, I notice a 38MB reduction in memory usage when closing the Flutter window.

    aab24aea-e976-4071-9e91-bb97c767c583

    before:
    20231212214121

    after:
    20231212220124

    I'm curious as to why this approach wasn't implemented previously? Am I wrong to change it this way? If this change is indeed beneficial and doesn't introduce any issues, I would be glad to contribute a fix for this problem.

  14. removed their assignment
    on Mar 27, 2024
  15. added
    team-windowsOwned by the Windows platform team
    and removed on Jun 6, 2024
  16. flutter-triage-bot commented on Jun 6, 2024

    @flutter-triage-bot

    The triaged-desktop label is irrelevant if there is no team-desktop label or fyi-desktop label.

  17. github-actions commented on Aug 1, 2024

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  18. locked as resolved and limited conversation to collaborators on Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: desktopRunning on desktope: OS-version specificAffects only some versions of the relevant operating systemfound in release: 3.16Found to occur in 3.16found in release: 3.17Found to occur in 3.17has reproducible stepsThe issue has been confirmed reproducible and is ready to work onperf: memoryPerformance issues related to memoryplatform-windowsBuilding on or for Windows specificallyteam-windowsOwned by the Windows platform teamtriaged-windowsTriaged by the Windows platform team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions