Windows Phone 8.1中的ListView存在内存泄漏问题吗?

4

我有一个绑定到对象集合(包含图像URI)的ListView。当我向ListView中添加更多项时,我的内存使用量会急剧增加。我认为问题在于UserModel的imageUri。请参见下面的代码:

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


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

照片模型
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);
        }
    } 

}

ListView的Xaml绑定

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

列表视图模板

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

如您所见,我的ListView数据模板有两个图像,一个用于实际照片,另一个用于用户。这两者都显示正确,但是当我继续向列表中添加更多项时,内存占用量会急剧上升。
请参见以下图片:高内存占用量 然而,如果我不设置UserModel.imageUri(对于所有PhotoModels,UserModel.imageUri为空),我就看不到内存占用量的急剧增长。
请参见以下图片:低内存占用量 这两个配置文件都执行相同的操作,加载相同的图像(共15张)。第一张照片带有用户照片,第二个截图没有。
我认为问题与PhotoModel具有UserModel并进行Set(...)有关。如下图所示,属性更改事件处理程序计数为140。
请参见以下图片:Profile1 enter image description here 其中大多数是PhotoModels,但我最多只有15个PhotoModels在集合中。我使用两个扩展方法进行清除和重新添加(可能会引起问题)。
     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);
        }
    }

我真的很希望能得到一些关于如何更好地处理性能问题以及这是否是内存泄漏的建议。


遇到了同样的问题。我似乎已经将其缩小到Ellipse.Fill中的ImageBrush。当我用普通图像替换椭圆时,使用的内存要少得多。看看你是否也是这种情况。 - Padraic
1个回答

1
问题出在ImageBrush如何解码较大的图像上。您需要设置BitmapImageDecodePixelHeightDecodePixelWidth
来源:Credit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接