简体   繁体   中英

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:

        <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...

I'm sorry if it's a basic question but I'm a newbie in C#.

Any hints?

(Maybe a way to dispose the LongListSelector or the whole page when the user hits the back button)

Thank You.

Last time I had this problem I used the IDipose pattern. The problem I had was when using third party dll's, the CLR was not cleaning the objects from the 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. More information on the using statement: 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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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