简体   繁体   English

内存泄漏:MVVM和LongListSelector(WP8)上的照片

[英]Memory Leak: MVVM and photos on a LongListSelector (WP8)

I'm trying to bind some (HD) photos to a LongListSelector as you can see here: 我正在尝试将一些(HD)照片绑定到LongListSelector,如您在此处看到的:

        <phone:LongListSelector
            Name="Photos"
            LayoutMode="Grid"
            ItemsSource="{Binding Items}"
            GridCellSize="225, 225"                
            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Image Name="Photo"
                         Source="{Binding Source}"
                    </Grid>
                </DataTemplate>
            </phone:LongListSelector.ItemTemplate>
        </phone:LongListSelector>



            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                foreach (string item in resultList)
                {
                    u = new Uri(item, UriKind.RelativeOrAbsolute);
                    this.Items.Add(new DataItemViewModel() { Source = u});
                }                    
            });            

but everytime I go back to the main page and click to start this all over again (with new/different photos) the ApplicationPeakMemoryUsage just keeps increasing until the app terminates itself... 但是每次我回到主页并单击以重新开始时(使用新的/不同的照片)时,ApplicationPeakMemoryUsage一直在增加,直到应用程序终止为止...

I'm sorry if it's a basic question but I'm a newbie in C#. 很抱歉,这是一个基本问题,但是我是C#的新手。

Any hints? 有什么提示吗?

(Maybe a way to dispose the LongListSelector or the whole page when the user hits the back button) (也许是当用户单击“后退”按钮时处置LongListSelector或整个页面的一种方法)

Thank You. 谢谢。

Last time I had this problem I used the IDipose pattern. 上次遇到此问题时,我使用了IDipose模式。 The problem I had was when using third party dll's, the CLR was not cleaning the objects from the dll. 我遇到的问题是使用第三方dll时,CLR没有从dll中清除对象。

After I implemented the IDispose pattern and whenever I used that specific object, I put it within the 'using' statement which ensured that it was disposed of at the end of the 'using' statement's scope. 在实现IDispose模式之后,无论何时使用该特定对象,我都会将其放入“ using”语句中,以确保在“ using”语句作用域的末尾对其进行处理。 More information on the using statement: http://www.dotnetperls.com/using 有关using语句的更多信息: http : //www.dotnetperls.com/using

To make me realise this in the first place I debugged my code and watched Task Manager. 为了使我首先意识到这一点,我调试了代码并观看了任务管理器。 Make a breakpoint at the start of the code, and open up task manager and click on the Processes tab. 在代码的开头创建一个断点,然后打开任务管理器,然后单击“进程”选项卡。 After certain points where you think the problem may lie, watch where the memory usage increases a lot. 在您认为问题可能出在某些地方之后,请注意内存使用量大量增加的地方。

Get back to me when you have done this. 完成此操作后,请尽快与我联系。

Hope it helps, get back to me if nothing works :) 希望对您有所帮助,如果没有任何帮助,请尽快与我联系:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM