难道的ListCollectionView泄漏内存? [英] Does ListCollectionView leak memory?

查看:206
本文介绍了难道的ListCollectionView泄漏内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究如何避免因从视图模型 INotifyCollectionChanged 事件的强引用的内存泄漏。我在玩弄使用的ListCollectionView 来看看是否能够处理它适合我。我认为以下是泄漏内存,我是不是做错了什么。



  VAR东西=新的ObservableCollection<串>(); 
,而(真)
{
VAR的结果=新的ListCollectionView(东西);
//只是为了确保内存我看到
//不等待GC'd
GC.Collect的();
}


解决方案

我最初发布这个作为发表评论,但我认为它使一个更好的答案,所以......



A),如果你确信你找到与.NET Framework的一个问题,你'重可能做错了什么。这不是不可能的,它只是不太可能。
b)该GC.Collect的()是不会做你在想什么会。



我认为你需要检查GC.Collect的()是如何工作的。






MSDN GC.Collect的方法



说明



使用这种方法来尝试回收所有的内存,无法访问。



所有对象,不论他们如何早已在存储器中,被认为是收集;然而,在托管代码引用的对象没有收集。使用此方法可以强制系统尝试回收可用内存的最大数量。






对于初学者来说,你不T显示我们你在哪里处理该内存的的ListCollectionView(东西)的。你只是分配新的分配新的,但你永远不处理旧的。所以,是的,它会泄露像疯了似的。直到GC运行和的尝试的收集。



如果你这样做,你用绳子将最有可能做的列表,这里展示的同样的事情一样的东西。但你已经证明什么,我希望它泄漏。


I've been investigating how to avoid memory leaks caused by strong references to the INotifyCollectionChanged event from a view model. I was playing around with using a ListCollectionView to see if that would deal with it for me. I think that the following is leaking memory, am I doing something wrong?

var stuff = new ObservableCollection<string>();
while (true)
{
    var result = new ListCollectionView(stuff);
    // Just to keep make sure that the memory I'm seeing 
    // isn't waiting to be GC'd
    GC.Collect(); 
}

解决方案

I initially posted this as a comment, but I think it makes a better answer, so ...

a) if you're sure you've found a problem with the .NET framework, you're probably doing something wrong. It's not impossible, it's just not likely. b) that GC.Collect() isn't going to do what you're thinking it will.

I think you need to review how GC.Collect() works.


MSDN GC.Collect Method

Remarks

Use this method to try to reclaim all memory that is inaccessible.

All objects, regardless of how long they have been in memory, are considered for collection; however, objects that are referenced in managed code are not collected. Use this method to force the system to try to reclaim the maximum amount of available memory.


For starters, you don't show us where you're disposing of that memory that the ListCollectionView(stuff). You're just allocating new and allocating new, but you never dispose of the old. So yeah, it's going to leak like crazy. Until the GC runs and tries to collect.

If you do the same thing you demonstrate here with a list of strings it will most likely do the same thing. But for what you've shown, I expect it to leak.

这篇关于难道的ListCollectionView泄漏内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆