WPF内存泄漏 [英] WPF Memory Leak

查看:210
本文介绍了WPF内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF形式,我自己并没有创造,所以我不是很擅长WPF。这是漏水严重,虽然,高达400 MB和关闭形式没有帮助。

问题出在我的应用程序加载的所有图片一次。我想只加载目前可见的。这是约300张照片,他们是有点大,所以我的WPF的形​​式从加载它们都受到影响。

我有一个的DataTemplate 用我自己的类型,它有一个属性缩略图。在code中的模板是这样的:

 <图像源={绑定路径=缩略图}拉伸=填充/>
 

然后,我有具有上述模板作为源的控制网格。在code这个控制是如下。请为我提供关于如何优化code,也许得到的唯一可见的,只有有许多控制在同一时间下载提示?

 < setter属性=模板>
        < Setter.Value>
            <的ControlTemplate的TargetType =控制:ElementFlow>
                <网格背景={TemplateBinding背景}>
                    <帆布X:名称=PART_HiddenPanel
                            IsItemsHost =真
                            能见度=隐藏/>
                    < Viewport3D X:名称=PART_Viewport>
                        <! - 相机 - >
                        < Viewport3D.Camera>
                            < PerspectiveCamera fieldOfView(视野)=60
                                               位置=0,1,4
                                               LookDirection =0,-1,-4
                                               UpDirection =0,1,0/>
                        < /Viewport3D.Camera>

                        < ContainerUIElement3D X:名称=PART_ModelContainer/>

                        < ModelVisual3D>
                            < ModelVisual3D.Content>
                                < AmbientLight颜色=白/>
                            < /ModelVisual3D.Content>
                        < / ModelVisual3D>
                < Viewport2DVisual3D
        RenderOptions.CachingHint =缓存
        RenderOptions.CacheInvalidationThresholdMaximum =2
        RenderOptions.CacheInvalidationThresholdMinimum =0.5/>
                    < / Viewport3D>
                < /网格>
            < /控件模板>
        < /Setter.Value>
    < /二传手>
< /样式和GT;
 

解决方案

是'ElementFlow控制描述的同一个的此处?看来,控制已经在使用虚拟化的,所以我不希望它来访问缩略图不可见项目的属性。

你是如何建模的数据结构暴露的缩略图属性?你可以将其设置使得房地产需求负荷首次访问缩略图?也许,实施这个一张背衬缓存(即保持加载在一段时间内的缩略图)将解决这一问题。

修改

我可以假设的东西我不应该。在阅读评论的第二个职位我联系,我现在认为这可能是ElementFlow控制的公开可用的版本不,事实上,实施虚拟化。也许你可以登录进入缩略图属性并确定属性被访问为不可见元素。

I have an WPF form that I myself did not create, so I am not very good at WPF. It is leaking badly though, up to 400 MB and closing the form does not help.

The problem lies in my application loading all the pictures at once. I would like to only load the ones visible at the moment. It is about 300 pictures and they are a bit large so my WPF-form suffers from loading them all.

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

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

And then I have a grid with a control that has the above template as source. The code for this control is the below. Please provide me with hints on how to optimize the code and perhaps get the only ones that are visible and only have that many 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>

解决方案

Is the 'ElementFlow' control the same one described here? It appears that control is already using virtualization, so I wouldn't expect it to access the Thumbnail property of a non-visible item.

How are you modelling the data structure that exposes the 'Thumbnail' property? Can you set it up such that the property demand-loads the thumbnail on first access? Perhaps implementing this with a backing cache (that keeps the thumbnails loaded for a period of time) would address the issue.

EDIT

I may have assumed something I should not have. In reading the comments to the second post I linked, I now think it may be that the publicly available version of the ElementFlow control does not, in fact, implement virtualization. Perhaps you could log access to the 'Thumbnail' property and determine if the property is accessed for non-visible elements.

这篇关于WPF内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆