c# – 带有ObservableCollection的Windows Phone 8.1 WinRT内存泄漏

我正在处理在MapControl上显示的大量对象(PO​​I).我正在帮助自己使用MVVM Light来遵守MVVM方法的规则.

由于我有义务在地图上显示每个对象,我必须使用MapItemsControl集合,而不是MapElements集合.
该集合绑定到ObservableCollection< PushpinViewModel>对象(Pushpins)在相应的ViewModel中.当我想要刷新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()方法.但是……没有任何反应!图钉被清除,但PushpinViewModel的终结器未被调用!然后我再次添加1000个元素,我的内存使用量翻倍.
你可以猜到接下来会发生什么.当我重复这个Clear() – Add()程序3-5次我的应用程序崩溃.

那么,问题是什么?显然,ObservableCollection在对其执行了Clear()之后保持对PushpinViewModel对象的引用,因此它们不能被垃圾回收.当然,强制GC执行垃圾收集并没有帮助(有时甚至会使情况变得更糟).

现在困扰我2天了,我尝试了许多不同的场景来尝试克服这个问题,但说实话,没有任何帮助.
只有一件事物毫无价值 – 我不记得确切的情况,但是当我指定了Pushpins = null,然后做了更多的事情时,VehiceViewModel被摧毁了.但这对我不起作用,因为我还记得在Clear()之后我在地图上可视化这些引脚时遇到了问题.

你有什么想法会导致这种内存泄漏吗?我如何强迫OC的成员摧毁?也许OC有某种替代方案?
在此先感谢您的帮助!

编辑:

我使用XAML Map Control-https://xamlmapcontrol.codeplex.com/进行了一些测试,结果令人惊讶.添加了> 1000个元素的整体地图性能比原生MapControl差,但是,如果我调用Add()x1000,然后调用Clear(),然后添加()x1000,则PushpinViewModel的终结器调用!内存被释放,应用程序不会崩溃.所以微软的MapControl肯定有问题……

解决方法

好的,这是我模仿MapItemsControl所做的行为.请注意,这是非常未经测试的 – 它适用于我的应用程序,但实际上并没有在其他任何地方尝试过.我从未测试过RemoveItems函数,因为我的应用程序只是将项添加到ObservableCollection并清除它们;它永远不会逐步删除项目.

另请注意,它使用绑定的项的哈希码标记XAML图钉;这是它如何识别集合更改时从地图中删除哪些图钉.这可能不适用于您的情况,但似乎是有效的.

用法:

注意:NumberedCircle是一个用户控件,它只是一个红色圆圈,在其中显示一个数字;替换为您想要用作图钉的任何XAML控件.目标是我的ObservableCollection对象,它具有Number属性(显示在图钉内)和Point属性(图钉位置).

<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),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
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


1:最直白的循环遍历方法,可以分为遍历key--value键值对以及所有的key两种表现形式2:用Linq的方式去查询(当然了这里要添加对应的命名空间 using System.Linq)如下为一个十分简单的代码示例:private void GetDicKeyByValue(){ Dicti...
private void ClearTextBox(){ foreach (var control in pnlDetail.Controls) { if (!(control is TextBox)) continue; var txtBox = (TextBox)control; txtBox.
原文叫看《墨攻》理解IOC概念 2006年多部贺岁大片以让人应接不暇的频率纷至沓来,其中张之亮的《墨攻》算是比较出彩的一部,讲述了战国时期墨家人革离帮助梁 国反抗赵国侵略的个人英雄主义故事,恢宏壮阔,浑雄凝重的历史场面相当震撼。其中有一个场景:当刘德华所饰的墨者革离到达梁国都城 下,城上梁国守军问:
System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
Request.Form value was detected from the client在使用ASP.NET MVC3 开发系统的用了百度的UEditor编辑器提交表单时遇到检测到有潜在危险的 Request.Form,我百度一下,试了网上的方法,都没用。要在Web.config增加&lt;h
右击文件夹-&gt;安全选项卡-&gt;添加-&gt;高级-&gt;立即查找Windows Server 2003:请您在目录添加IIS来宾帐号(IUSR_Hostname)的只读权限,以及Network Service组的读写修改权限。Windows Server 2000:请您在目录添加IIS来
&lt;compilationdebug=&quot;true&quot;&gt;&lt;buildProviders&gt;&lt;addextension=&quot;.html&quot;type=&quot;System.Web.Compilation.PageBuildProvider&q
在ASP.NET MVC 中 Spring.NET 配置注入的时候,下面这方式是可行的。&lt;spring&gt; &lt;context&gt; &lt;resource uri=&quot;config://spring/objects&quot; /&gt; &lt;/context&gt;
Stopwatch stopwatch = new Stopwatch();stopwatch.Start();。。。。。中间代码。。。&#x9;stopwatch.Stop();&#x9;long result = stopwatch.ElapsedMilliseconds;sqlBulkCopy.Close()
问题描述 在asp.net mvc 下配置ueditor图片上传时总是提示:缺少十六进制字符错误(IE下提示),起初还以为是我上传的图片名称中有中文字符所致,后来我又上传了英文字符名字的图片发现还是一样的错误提示:◆无赖之下只好到火狐下看错误的详情:◆第二个错误我无法解决,先看第一个,输入地址显示错
已经安装net2.0 和3.5 ,但IIS里面却只有1.1开始→运行→CMDC:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i按回车键后便会开始自动安装,安装完重启一下IIS在IIS中ASP.NET选项卡便可以看到。注
根据传进来不同的值,调用不同的方法View Code protected void btn_SwitchClick(object sender, EventArgs e){ string result = &quot;&quot;; switch (ddlMethod.SelectedValue)
整理两个 在C#中,用正则表达式 获取网页源代码标签的属性或值的方法 :1、获取标签中的值: CSDN 结果:CSDN/// /// 获取字符中指定标签的值 /// /// 字符串 /// 标签 /// 值 public static string GetTitleContent(string st
/// &lt;summary&gt;/// 集合装换DataTable/// &lt;/summary&gt;/// &lt;param name=&quot;list&quot;&gt;集合&lt;/param&gt;/// &lt;returns&gt;&lt;/returns&gt;publ
将目录下面所有的 .cs 文件合并到一个 code.cs 文件中,写著作权复制代码时的必备良药 @echo off echo 将该目录下所有.cs文件的内容合并到一个 code.cs 文件中! pause dir /ad/s/b &gt; folderPath.txt md codeTemp for
做接口开发的时候,往往接受参数或返回值是一个XML的字符串。如下图,不方便辨识 两种方法, 1.将它保存为xxx.xml,然后用浏览器打开。这种方法稍微有些麻烦。 2.使用 UltraEdit 工具
一个字段控制多个状态选项private void GenerateAdvice_OnClick(object sender, RoutedEventArgs e){ TestStatus c1 = TestStatus.A | TestStatus.C | TestStatus.E; v...
效果如下: 代码如下:
StartUp.cs public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseEndpoints(endpoints =&gt; { endpoints.MapControllerRoute(