WPF memory leak

I have a WPF form that I have not created myself, so I am not very good at WPF. It runs poorly, although up to 400 MB and closing the form does not help.

The problem is my application downloading all the pictures at once. I would like to download only those that are currently visible. These are about 300 photos, and they are a bit large, so my WPF form suffers from loading them.

I have a DataTemplate with my own type that has a Thumbnail property. The code in the template looks like this:

  <Image Source="{Binding Path=Thumbnail}" Stretch="Fill"/> 

And then I have a grid with a control that has the above template as its source. The code for this control is shown below. Please provide me with tips on how to optimize the code and possibly only get the ones that are visible, and only the same number of controls loaded at the same time?

  <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Controls:ElementFlow"> <Grid Background="{TemplateBinding Background}"> <Canvas x:Name="PART_HiddenPanel" IsItemsHost="True" Visibility="Hidden" /> <Viewport3D x:Name="PART_Viewport"> <!-- Camera --> <Viewport3D.Camera> <PerspectiveCamera FieldOfView="60" Position="0,1,4" LookDirection="0,-1,-4" UpDirection="0,1,0" /> </Viewport3D.Camera> <ContainerUIElement3D x:Name="PART_ModelContainer" /> <ModelVisual3D> <ModelVisual3D.Content> <AmbientLight Color="White" /> </ModelVisual3D.Content> </ModelVisual3D> <Viewport2DVisual3D RenderOptions.CachingHint="Cache" RenderOptions.CacheInvalidationThresholdMaximum="2" RenderOptions.CacheInvalidationThresholdMinimum="0.5"/> </Viewport3D> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> 
+4
source share
3 answers

Is the "ElementFlow" control the same as described here ? It looks like management is already using virtualization , so I did not expect it to gain access to the Thumbnail property for an invisible element.

How do you simulate a data structure that provides a Thumbnail property? Can you customize it so that a property request loads a thumbnail on first access? Perhaps implementing this with a support cache (which retains thumbnail thumbnails for a specific period of time) will fix the problem.

EDIT

Perhaps I suggested that I should not have done this. When reading the comments on the second post that I linked, I now think that the publicly available version of the ElementFlow control does not actually implement virtualization. Perhaps you can register access to the Thumbnail property and determine if access to invisible elements is available.

+2
source

The first thing you need to pay attention to when you try to find memory leaks in a .NET, WPF application or not are objects that subscribe to events.

If object X listens for the event raised by object Y, then Y contains a reference to X. Regardless of which virtualization (or deletion) method you implement, if X does not cancel the subscription to the Y event, X will remain in the object for the time being until while Y does, and will never be completed and collected from the trash. (Even if it implements IDisposable , and you explicitly call Dispose on it.)

When you say that β€œclosing the form does not help”, it makes me doubly suspicious: I expect someone to inject an object property into the Window object, and this object subscribes to some event. Thus, you close the window, but it still exists in the object graph, because it refers to one of its properties.

(To give you an idea of ​​how cunning it can be: WinForms ToolStrip controls the subscription to Windows theme change events when they become visible. It's great that the theme change on your computer is automatically reflected in your user interface. It's not so great if you find ToolStrip without first setting Visible to false, it will continue to receive theme change events until the application terminates.)

A memory profiler can help with this - since I found that my application contained thousands of ToolStrip objects in memory, although I thought they were all destroyed.

+4
source

It is difficult to narrow down the problem with just a piece of code. You might want Visual Profiler if you haven’t already.

0
source

Source: https://habr.com/ru/post/1310165/


All Articles