Repository navigation
Open
Labels
Milestone
Description
- .NET Core Version: 3.1.100
- Windows version: 18363
- Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
- Is this bug related specifically to tooling in Visual Studio No
Problem description:
The image source will memory leak when we remove the image from visual tree before we set the image source as null
Step:
- Set the image source
- Add image in the VisualTree and wait the image show
- Remove image from the visual tree and then set the image source as null
And you can find the image source object never free
private void Button_Click(object sender, RoutedEventArgs e)
{
// Remove the current Image control from the visual tree and set source is null when click button.
// Then new a image control and add source to the RenderTargetBitmap object and show it.
// You can see the gc never delete the RenderTargetBitmap object that make memory leak.
var oldBorder = RootGrid.Children.OfType<Border>().LastOrDefault();
if (oldBorder != null)
{
var oldImage = (Image)oldBorder.Child;
// In order to solve it , you should set the image.Source is null and use UpdateLayout.
// The below code can solve it.
// oldImage.Source = null;
// oldImage.UpdateLayout();
// Remove the current Image control from the visual tree.
RootGrid.Children.Remove(oldBorder);
oldImage.Source = null;
Borders.Add(oldBorder);
}
var bitmap = new RenderTargetBitmap(1024, 1024, 96, 96, PixelFormats.Default);
var image = new Image { Source = bitmap };
var border = new Border { Child = image };
RootGrid.Children.Add(border);
// In order to facilitate changes in memory, after each operation will be garbage collection
GC.Collect();
}
public readonly List<Border> Borders = new List<Border>();
Actual behavior:
The Image memory leak
Expected behavior:
The image source object can be free
Minimal repro:
Reacted by walterlv, huchengv5, Sean, Tobias Käs, h82258652, laggage, Ivan Macias, Sebastien Mouy, 536b6f74746b69, afunc and 3 more
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsTodo
Activity
lindexi commentedon Jan 5, 2020
How to solve it?
In order to solve it , you should set the image.Source is null and use UpdateLayout.
The below code can solve it.
lindexi commentedon Jan 5, 2020
Why the image will memory leak?
Because the Image use RenderData class to storage the image source object and the
UIElement._drawingContentwill reference the RenderData.The image will never re-render when we remove the image from the visual tree and the RenderData will never update.
Why we set the image source as null and then update layout can solve?
Because the image source is null and the update layout will call render to update the
_drawingContentto the new RenderData which does not storage the image source object (or it stroage a null object)meJevin commentedon Oct 27, 2021
@lindexi, you are amazing! Thank you so much for providing a solution as well as an explanation!
[-]Known issus: WPF Image memory leak when remove image from visual tree[/-][+]Known issue: WPF Image memory leak when remove image from visual tree[/+]Kuldeep-MS commentedon Oct 10, 2022
Hi @lindexi
Thank you for the insights you have provided. However, when I tried to reproduce the issue, it seems the increase in memory is due to the oldBorder element retention at line #38.
Please share more information such as memory dump taken at different intervals which shows the memory increase (after commenting out the aforementioned line 38).
This will help us repro the issue at our end and will help us narrow down the problem.
lindexi commentedon Oct 11, 2022
@Kuldeep-MS Thank you.
Right. We must keep the Image control alive, and we should remove the current Image control from the visual tree. The
Borders.Add(oldBorder)will add the reference ofoldBorder. And theoldBorderreferences Image control.But the
Imagecontrol still reference the RenderTargetBitmap after we set the souce to null byoldImage.Source = nullIn other words, the Image control not being recycled is as expected. But the
RenderTargetBitmapnot being recycled is not as expected.It means that the
RenderTargetBitmapcan not be deleted from memory.And I think we can fix this by #7177