简体   繁体   English

ListView内存泄漏Windows Phone 8.1?

[英]ListView Memory Leak Windows Phone 8.1?

I've got a ListView bound to an ObservableCollection of objects(containing image URI's), When I add more items to the ListView I am seeing a massive spike in memory. 我有一个ListView绑定到对象的ObservableCollection(包含图像URI的对象),当我向ListView中添加更多项时,我看到内存大量增加。 I think I have narrowed its down to being a problem with the UserModel's imageUri. 我认为我已将其范围缩小为UserModel的imageUri问题。 see below. 见下文。

public class UserModel : ObservableObject
{
    ...
     private string _imageUri;
    ...


    ...
    public string ImageUri
    {
        get
        {
            return _imageUri;
        }
        set
        {
            Set(() => ImageUri, ref _imageUri, value);
        }
    } 
}

photo model 照片模型

public class PhotoModel : ObservableObject
{
    ...
    private UserModel _user;
    private string _imageUri;
    ...

    ...
    public UserModel User
    {
        get
        {
            return _user;
        }
        set
        {
            Set(() => User, ref _user, value);
        }
    }

    public string ImageUri
    {
        get
        {
            return _imageUri;
        }
        set
        {
            Set(() => ImageUri, ref _imageUri, value);
        }
    } 

}

Xaml Binding of ListView Xaml绑定的ListView

<ListView
      x:Name="MostPopularListView"
      ItemsSource="{Binding PhotosCollection}"
      ItemTemplate="{StaticResource MostPopularDataTemplate}"
      Margin="0,0,0,0"
      IsItemClickEnabled="True"/>

Listview template Listview模板

       ...
       <Image 
          Source="{Binding ImageUri}"             
          Stretch="Fill" 
          Height="300" />

       ...

       <Ellipse 
            Width="40"
            Height="40" 
            Margin="10,0,0,10">
            <Ellipse.Fill>
                 <ImageBrush>
                     <ImageBrush.ImageSource>
                         <BitmapImage UriSource="{Binding User.ImageUri}" />
                            </ImageBrush.ImageSource>
                      </ImageBrush>
                 </Ellipse.Fill>
            </Ellipse>
            ...

As you can see my ListView data template has two images, one for the actual photo and one for the user. 如您所见,我的ListView数据模板有两张图像,一张用于实际照片,另一张用于用户。 Both are these are showing correctly but when I continue to add more items to the list I am seeing a massive spike in memory. 两者都显示正确,但是当我继续向列表中添加更多项目时,我看到内存急剧增加。

See image: 见图片: 高内存配置文件

However if I don't set UserModel.imageUri (UserModel.imageUri is null then for all PhotoModels) I dont see this spike in memory. 但是,如果我未设置UserModel.imageUri(UserModel.imageUri为null,则对于所有PhotoModels),我不会在内存中看到此峰值。 低内存配置文件

Both profiles are carrying out the same actions loading the same images (total 15). 两个配置文件均执行相同的操作以加载相同的图像(共15个)。 First photo is with user photos and second screenshot is without. 第一张照片带有用户照片,第二张屏幕截图没有用户照片。

I think the problem is something to do with PhotoModel having a UserModel and doing Set(...). 我认为问题与PhotoModel具有UserModel并执行Set(...)有关。 As you can see from the photos below property change event handler has a count of 140. 从下面的照片中可以看到,属性更改事件处理程序的计数为140。

资料1 在此处输入图片说明

Most of these are PhotoModels but i only ever have 15 PhotoModels max in the Collection. 其中大多数是PhotoModels,但我在收藏中最多只能有15个PhotoModels。 I do clear and re-add using two extension methods (could thee be causing it). 我确实使用两种扩展方法清除并重新添加了(可能是由它引起的)。

     public static void Repopulate<T>(this ICollection<T> collection, IEnumerable<T> items)
    {
        collection.Clear();
        foreach (var item in items)
        {
            collection.Add(item);
        }
    }

    public static void AddObjects<T>(this ICollection<T> collection, IEnumerable<T> items)
    {
        foreach (var item in items)
        {
            collection.Add(item);
        }
    }

I'd really appreciate some advice on how to handle the performance better and if this is a memory leak. 我非常感谢您提供一些有关如何更好地处理性能以及是否存在内存泄漏的建议。

The problem is with how ImageBrush decodes the larger image. 问题在于ImageBrush如何解码较大的图像。 You need to set the DecodePixelHeight and DecodePixelWidth of the BitmapImage 您需要设置BitmapImageDecodePixelHeightDecodePixelWidth

Credit: http://timheuer.com/blog/archive/2015/05/06/making-circular-images-in-xaml-easily.aspx 图片来源: http : //timheuer.com/blog/archive/2015/05/06/making-circular-images-in-xaml-easily.aspx

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

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