Windows Phone 8.1 WinRT中ObservableCollection存在内存泄漏问题

6
我正在处理大量的对象(POI),这些对象会显示在一个地图控件上。我使用MVVM Light来遵循MVVM方法的规则。
由于我必须在地图上显示每个对象,所以我必须使用MapItemsControl集合,而不是MapElements集合。该集合绑定到相应ViewModel中的ObservableCollection 对象(Pushpins)。一切都按预期工作,直到我想要刷新Pushpins。问题是内存泄漏。但首先,让我们看一下代码来可视化问题:
XAML:
<maps:MapControl x:Name="Map"
                 x:Uid="MapControl">
  <maps:MapItemsControl ItemsSource="{Binding Pushpins}">
    <maps:MapItemsControl.ItemTemplate>
      <DataTemplate>
        <Image Source="{Binding Image}"/>
      </DataTemplate>
    </maps:MapItemsControl.ItemTemplate>
  </maps:MapItemsControl>

MainViewModel:

public class MainViewModel : ViewModelBase
{
    public RelayCommand AddCommand { get; set; }
    public RelayCommand ClearCommand { get; set; }
    public RelayCommand CollectCommand { get; set; }

    public ObservableCollection<PushpinViewModel> Pushpins { get; set; }

    /* Ctor, initialization of Pushpins and stuff like that */

    private void Collect()
    {
        GC.Collect(2);
        GC.WaitForPendingFinalizers();
        GC.Collect(2);
        PrintCurrentMemory();
    }

    private void Clear()
    {
        Pushpins.Clear();
        PrintCurrentMemory();
    }

    private void Add()
    {
        for (int i = 0; i < 1000; i++)
        {
            Pushpins.Add(new PushpinViewModel());
        }
        PrintCurrentMemory();
    }

    private void PrintCurrentMemory()
    {
        Logger.Log(String.Format("Total Memory: {0}", GC.GetTotalMemory(true) / 1024.0));
    }
}

PushpinViewModel:

public class PushpinViewModel: ViewModelBase
{
    public string Image { get { return "/Assets/SomeImage.png"; } }

    ~PushpinViewModel()
    {
        Logger.Log("This finalizer never gets called!");
    }
}

现在考虑以下情况。我向“Pushpins”集合中添加了1000个“PushpinViewModel”元素并进行了渲染,分配了内存,一切都很好。现在我想清除该集合,并添加其他(在实际情况下不同的)1000个元素。所以,我调用了“Clear()”方法。但是...什么也没发生!“Pushpins”被清除了,但“PushpinViewModel”的终结器没有被调用!然后我再次添加了1000个元素,我的内存使用量加倍了。
你可以猜到接下来会发生什么。当我重复执行此“Clear()” - “Add()”过程3-5次时,我的应用程序崩溃了。
那么问题出在哪里呢?显然,“ObservableCollection”在对其执行“Clear()”操作后保持对“PushpinViewModel”对象的引用,因此它们无法进行垃圾回收。当然,强制GC执行垃圾回收没有帮助(有时甚至会使情况变得更糟)。
这让我困扰了2天,我尝试了许多不同的场景来尝试克服这个问题,但说实话,没有什么帮助。只有一件值得注意的事情——我不记得确切的情况了,但当我分配“Pushpins=null”,然后做了一些其他事情时,“VehiceViewModel”被销毁了。但这对我来说行不通,因为我也记得在“Clear()”之后视觉上无法在地图上显示这些引脚。
你有任何想法是什么导致了这个内存泄漏吗?我如何强制“OC”的成员销毁?也许有一种替代“OC”的方法?
提前感谢任何帮助!
编辑:
我进行了一些XAML地图控制的测试-https://xamlmapcontrol.codeplex.com/,结果令人惊讶。总体而言,添加了超过1000个元素的地图性能比本机“MapControl”差,但是,如果我调用“Add()”x1000,然后“Clear()”,然后“Add()”x1000,“PushpinViewModel”的终结器就会被调用!内存被释放,应用程序不会崩溃。因此,微软的“MapControl”肯定存在问题...

问题很可能是您加载到表单上的位图被缓存到内存中。 GC 的收集不会将其从缓存中删除。 请参阅这些 相关的问题。 - Scott Chamberlain
谢谢你的回答。我正在调查这个可能性,并同意它确实可能是一个原因。我已经检查了你提供的链接,不幸的是那里发布的答案大多适用于WPF应用程序(我在WinRT - Universal App中)。我成功地从流中加载了一张图片(作为异步方法,但它起作用了)- 不幸的是它没有帮助。内存消耗甚至更大:( - Malutek
1
我无法针对你的具体问题进行讲解,但我也曾遇到过MapItemsControl的一些问题。我的设置与你几乎完全相同,都是使用MVVM绑定ObservableCollection。如果我从地图导航到另一个视图,然后再回到地图,并重复这个过程3-5次,我通常会遇到一个“访问冲突”错误,我把它缩小到了MapItemsControl。我用一个行为来代替它,这个行为需要一个项目源,然后为我在地图上绘制推钉,如果你想让我发布源代码,请告诉我。 - Paul Abbott
如果这不是问题@paul.abbott.wa.us,我会非常感激。我已经联系了与Microsoft相关的人员,他们确认这是一个错误:http://social.msdn.microsoft.com/Forums/windowsapps/en-US/f6eea4f5-9f55-4d18-af40-88ac6de5c32f/mapcontrols-memory-leak-windows-phone-81-runtime-universal-app?forum=wpdevelop,并提出了解决方法,但是它并没有起作用。提前致谢! - Malutek
1个回答

8

好的,这里是我制作的模拟MapItemsControl行为的代码。请注意,这个代码并没有经过严格测试——它在我的应用程序中可以使用,但在其他地方还没有试过。另外,我从未测试过RemoveItems函数,因为我的应用程序只是向ObservableCollection中添加项目并清除它们;它从来没有逐步删除项目。

还要注意的是,它会用绑定的项目的哈希码标记XAML pushpin;这就是它在集合更改时识别要从地图上删除哪些pushpin的方法。这种方法可能不适用于您的情况,但它似乎是有效的。

用法:

注意:NumberedCircle是一个用户控件,它只是一个显示数字的红色圆圈;用任何你想要用作pushpin的XAML控件替换它。Destinations是我的包含Number属性(用于在pushpin内显示)和Point属性(pushpin位置)的对象的ObservableCollection

<map:MapControl>
   <i:Interaction.Behaviors>
      <behaviors:PushpinCollectionBehavior ItemsSource="{Binding Path=Destinations}">
         <behaviors:PushpinCollectionBehavior.ItemTemplate>
            <DataTemplate>
               <controls:NumberedCircle Number="{Binding Path=Number}" map:MapControl.Location="{Binding Path=Point}" />
            </DataTemplate>
         </behaviors:PushpinCollectionBehavior.ItemTemplate>
      </behaviors:PushpinCollectionBehavior>
   </i:Interaction.Behaviors>
</map:MapControl>

代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Xaml.Interactivity;

using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls.Maps;

namespace Foo.Behaviors
{
    /// <summary>
    /// Behavior to draw pushpins on a map.  This effectively replaces MapItemsControl, which is flaky as hell.
    /// </summary>
    public class PushpinCollectionBehavior : DependencyObject, IBehavior
    {
        #region IBehavior

        public DependencyObject AssociatedObject { get; private set; }

        public void Attach(Windows.UI.Xaml.DependencyObject associatedObject)
        {
            var mapControl = associatedObject as MapControl;

            if (mapControl == null)
                throw new ArgumentException("PushpinCollectionBehavior can be attached only to MapControl");

            AssociatedObject = associatedObject;

            mapControl.Unloaded += MapControlUnloaded;
        }

        public void Detach()
        {
            var mapControl = AssociatedObject as MapControl;

            if (mapControl != null)
                mapControl.Unloaded -= MapControlUnloaded;
        }

        #endregion

        #region Dependency Properties

        /// <summary>
        /// The dependency property of the item that contains the pushpin locations.
        /// </summary>
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(object), typeof(PushpinCollectionBehavior), new PropertyMetadata(null, OnItemsSourcePropertyChanged));

        /// <summary>
        /// The item that contains the pushpin locations.
        /// </summary>
        public object ItemsSource
        {
            get { return GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        /// <summary>
        /// Adds, moves, or removes the pushpin when the item source changes.
        /// </summary>
        private static void OnItemsSourcePropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var behavior = dependencyObject as PushpinCollectionBehavior;
            var mapControl = behavior.AssociatedObject as MapControl;

            // add the items

            if (behavior.ItemsSource is IList)
                behavior.AddItems(behavior.ItemsSource as IList);
            else
                throw new Exception("PushpinCollectionBehavior needs an IList as the items source.");

            // subscribe to changes in the collection

            if (behavior.ItemsSource is INotifyCollectionChanged)
            {
                var items = behavior.ItemsSource as INotifyCollectionChanged;
                items.CollectionChanged += behavior.CollectionChanged;
            }
        }

        // <summary>
        /// The dependency property of the pushpin template.
        /// </summary>
        public static readonly DependencyProperty ItemTemplateProperty =
            DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(PushpinCollectionBehavior), new PropertyMetadata(null));

        /// <summary>
        /// The pushpin template.
        /// </summary>
        public DataTemplate ItemTemplate
        {
            get { return (DataTemplate)GetValue(ItemTemplateProperty); }
            set { SetValue(ItemTemplateProperty, value); }
        }

        #endregion

        #region Events

        /// <summary>
        /// Adds or removes the items on the map.
        /// </summary>
        private void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    AddItems(e.NewItems);
                    break;

                case NotifyCollectionChangedAction.Remove:
                    RemoveItems(e.OldItems);
                    break;

                case NotifyCollectionChangedAction.Reset:
                    ClearItems();
                    break;
            }
        }

        /// <summary>
        /// Removes the CollectionChanged event handler from the ItemsSource when the map is unloaded.
        /// </summary>
        void MapControlUnloaded(object sender, RoutedEventArgs e)
        {
            var items = ItemsSource as INotifyCollectionChanged;

            if (items != null)
                items.CollectionChanged -= CollectionChanged;
        }

        #endregion

        #region Private Functions

        /// <summary>
        /// Adds items to the map.
        /// </summary> 
        private void AddItems(IList items)
        {
            var mapControl = AssociatedObject as MapControl;

            foreach (var item in items)
            {
                var templateInstance = ItemTemplate.LoadContent() as FrameworkElement;

                var hashCode = item.GetHashCode();

                templateInstance.Tag = hashCode;
                templateInstance.DataContext = item;

                mapControl.Children.Add(templateInstance);

                Tags.Add(hashCode);
            }
        }

        /// <summary>
        /// Removes items from the map.
        /// </summary>
        private void RemoveItems(IList items)
        {
            var mapControl = AssociatedObject as MapControl;

            foreach (var item in items)
            {
                var hashCode = item.GetHashCode();

                foreach (var child in mapControl.Children.Where(c => c is FrameworkElement))
                {
                    var frameworkElement = child as FrameworkElement;

                    if (hashCode.Equals(frameworkElement.Tag))
                    {
                        mapControl.Children.Remove(frameworkElement);
                        continue;
                    }
                }

                Tags.Remove(hashCode);
            }
        }

        /// <summary>
        /// Clears items from the map.
        /// </summary>
        private void ClearItems()
        {
            var mapControl = AssociatedObject as MapControl;

            foreach (var tag in Tags)
            {
                foreach (var child in mapControl.Children.Where(c => c is FrameworkElement))
                {
                    var frameworkElement = child as FrameworkElement;

                    if (tag.Equals(frameworkElement.Tag))
                    {
                        mapControl.Children.Remove(frameworkElement);
                        continue;
                    }
                }
            }

            Tags.Clear();
        }

        #endregion

        #region Private Properties

        /// <summary>
        /// The object tags of the items this behavior has placed on the map.
        /// </summary>
        private List<int> Tags
        {
            get
            {
                if (_tags == null)
                    _tags = new List<int>();

                return _tags;
            }
        }
        private List<int> _tags;

        #endregion
    }
}

感谢您提供的解决方案。 - Krzusztof
2
谢谢,我在重复2-3次后遇到了“访问冲突”错误,这个解决方案很有帮助。 - Alexandr
非常感谢,这帮了我大忙了。虽然在我切换到你的代码后,MapItemsControl DataTemplate 内部的某个命令停止工作了,我有一个问题。你有任何想法吗? - stambikk

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