Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory Leak in WPF ListBox with ListBoxItems in Navigation Windows #10348

Open
Kamalesh-Periyasamy opened this issue Jan 28, 2025 · 3 comments
Open
Labels
📭 waiting-author-feedback To request more information from author.

Comments

@Kamalesh-Periyasamy
Copy link

Description

When using a ListBox control with ListBoxItem in a WPF Navigation Window, opening and closing the window multiple times leads to memory leaks. The ListBox and its related objects are not released from memory, as confirmed using the .NET Memory Profiler.

Reproduction Steps

  1. Create a WPF Navigation Window with a ListBox containing ListBoxItems.
  2. Open the navigation window multiple times and close it.
  3. Use a memory profiler to observe memory allocations.
  4. Notice that ListBox, ListBoxItem, and other related objects like Event from ItemContainerGenerator and ItemCollection remain in memory.

Refer the attached sample to replicate the issue.
TextBoxExtSample.zip

Expected behavior

When the Navigation Window is closed, the ListBox and its child elements should be released, and garbage collection should reclaim the memory.

Actual behavior

The ListBox, ListBoxItem, and related objects like Event from ItemContainerGenerator and ItemCollection remain in memory, causing an accumulation of memory usage over time, leading to a memory leak.

Regression?

Not sure if this issue was present in previous versions of WPF.

Known Workarounds

No response

Impact

The memory leak leads to increased memory usage in applications that frequently open and close Navigation Windows, eventually causing performance degradation.

Configuration

No response

Other information

Image
Attached the reference image

@h3xds1nz
Copy link
Member

h3xds1nz commented Jan 28, 2025

I'd say this just a misunderstanding on how Garbage collection works. I've tested your sample briefly.

Instead of pressing a button; go big, add a DispatcherTimer to open the window each 400 ms for example; add this to your Window1 so you can see it was actually fully loaded for the full graphical image.

private async void Window_Loaded(object sender, RoutedEventArgs e)
{
    await Task.Delay(200); // cause eager event exit
    this.Close();
}

Image

And after you've opened and closed the window 10k times for example (up to you how long you want to wait), force the collection via snapshot, measure the objects and heap usage (or search for the mentioned ListBoxItem directly for example); you'll see you're back to the amount as when you've opened the window the first time.

See #9827 (comment) for example.

@himgoyalmicro
Copy link
Contributor

@Kamalesh-Periyasamy are we good to close this issue based on the comment by @h3xds1nz?

@himgoyalmicro himgoyalmicro added the 📭 waiting-author-feedback To request more information from author. label Jan 29, 2025
@Kamalesh-Periyasamy
Copy link
Author

@himgoyalmicro , I will check and acknowledge here.

@dotnet-policy-service dotnet-policy-service bot removed the 📭 waiting-author-feedback To request more information from author. label Jan 29, 2025
@himgoyalmicro himgoyalmicro added the 📭 waiting-author-feedback To request more information from author. label Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📭 waiting-author-feedback To request more information from author.
Projects
None yet
Development

No branches or pull requests

3 participants