question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Potential Memory Leak when using Popup

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

When opening a popup from a page defined in the AppShell xaml, the popup is never eligible for garbage collection. In a scenario where a popup is used repeatedly, this will lead to memory being allocated that can never be reclaimed.

When opening a popup from a page that has been pushed onto the navigation stack, the popup does become eligible for garbage collection, although it appears to only do so after the page is dismissed. If this is truly the case, that would explain why popups presented by Shell-defined pages cannot be collected since the Shell-defined pages exist for the lifetime of the app.

This does not appear to be platform specific; I was able to recreate on iOS, Android, and Windows.

Expected Behavior

Once a popup is dismissed (and has gone out of scope), it should be eligible for garbage collection.

Steps To Reproduce

  1. Open and run solution in reproduction repo.
  2. Add a breakpoint to the finalizer in CustomPopup.xaml.cs.
  3. Click the “Create popup, but don’t open” button
    • Click “Force Garbage Collection”
    • Notice breakpoint hits
  4. Click the “Open popup using MainPage” button
    • Dismiss the popup
    • Click “Force Garbage Collection”
    • Notice breakpoint does not hit.
  5. Click the “Open popup using New Page” button
    • Dismiss the popup
    • Return to Main page
    • Click “Force Garbage Collection”
    • Notice breakpoint hits

Link to public reproduction project repository

https://github.com/JustinC-IADS/PopupMemoryLeakRepo/tree/main/PopupMemoryLeak

Environment

- .NET MAUI CommunityToolkit: 1.3.0
- OS: Windows 11, iOS 16, Android (does not appear to be OS-releated)
- .NET MAUI: 6.0.400

Anything else?

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
JustinC-IADScommented, Dec 28, 2022

@pictos is correct. On Windows, I’m using the built-in VS diagnostic tools.

image

This example is intentionally extreme (the popup is allocating a large chunk of memory) to illustrate the point. Regardless of how much memory pressure there is or how many times GC runs, it is never able to reclaim any of the allocated memory.

1reaction
JustinC-IADScommented, Dec 28, 2022

@pictos, I’ve update the reproduction repo to .net7 and the latest version of the toolkit just now.

  1. Open the repo and search for “put breakpoint here”, you’ll find a console message inside of a custom popup finalizer.

  2. Run the program on any target (if you select Windows, you can see the memory usage grow in the profiler)

  3. There are 3 buttons that illustrate various scenarios (and a button to force GC). image

  4. The first button will create a popup, but not actually show it. This version allows the finalizer to run with no issue. Sometimes it is necessary to force GC to see it hit immediately, but the instance is obviously eligible for GC.

  5. The second button creates a popup and uses the current page to display it. This is the most problematic scenario. After the popup has been displayed, I can find no way for it to ever be garbage collected.

  6. The third button creates a popup and uses a new page to display it. From what I can tell, the popup will become eligible for GC after the new page is closed, but not before.

My working theory is that once a popup is shown, it does not become eligible for GC until the page that was used to show it is dismissed.

A couple of notes:

  1. The popup is a bit sluggish to initialize because it’s allocating a large amount of memory to illustrate the problem of the memory leak in the profiler.
  2. Sometimes it is necessary to click the “Force GC button” a few times for the finalizer to hit. No amount of clicking/waiting seems to fix the issue of not being able to collect the instance while the page is still displayed though.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Detached window memory leaks - web.dev
Detached windows are a common type of memory leak that is particularly difficult to find and fix.
Read more >
Potential memory leak occurs when LookUpEdit's popup ...
Hello, We investigated this issue and found a place that produced the small memory leaks I described in the previous comment. We fixed...
Read more >
4 Types of Memory Leaks in JavaScript and How to Get Rid ...
In essence, memory leaks can be defined as memory that is not required by an application anymore that for some reason is not...
Read more >
Android - Memory leak with popup window in Recyclerview
I'm having trouble with the following error displaying in my app: "E/WindowManager: android.view.WindowLeaked: Activity com.awt.myapp.MyList has ...
Read more >
[Suggestion Utility] Tippy.js: Potential memory leak with the ...
When an editor with the Mentions extension is unmounted while having the mention suggestion popup active and selected, this message appears ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found