如何释放内存使用在C#中的大名单? [英] How free memory used by a large list in C#?

查看:400
本文介绍了如何释放内存使用在C#中的大名单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为人口名单,是非常多位置的大名单,并在某些时候我停止使用。我该如何释放资源?
然后,这是部分代码:

I have a list called Population, is a great list of very many positions and at some point I stop using it. How I can free the resources? Then this is part of the code:

 private List <BasePopulation> Population=new List <BasePopulation>();
 Population.SomeMethod();
 Population.Clear();



我用Clear方法,而不是工作。任何想法?

I used the Clear method, but not works. Any idea?

推荐答案

该问题可能是清除不是做你认为它是。 清除只是标志着列表作为是没有调整,它在后台使用的内部数组空。它将,但是,删除所有引用到各个 BasePopulation 实例。因此,如果没有其他的数据结构有给他们参考,他们将有资格进行垃圾回收。但是,它会的不可以 直接降低列表的大小。我只是证实了这一使用ILSpy。

The problem may be that Clear is not doing what you think it is. Clear simply marks the List as being empty without resizing the internal array that it uses behind the scenes. It will, however, remove all references to the individual BasePopulation instances. So if no other data structure has a reference to them they will be eligible for garbage collection. But, it will not reduce the size of the List directly. I just verified this using ILSpy.

您有两种选择。


  1. 设置人口= NULL 。这将unroot整个对象实例使它符合垃圾收集。

  1. Set Population = null. This will unroot the entire object instance making it eligible for garbage collection.

呼叫 TrimExcess 在这个列表。这将调整内部数组。

Call TrimExcess on this List. This will resize the internal array.

这篇关于如何释放内存使用在C#中的大名单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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