什么时候分配新列表比 .Clear() 现有列表更合适?
给定一个列表:
List<myType> myList = new List<myType>();
在运行时修改集合内容......
为了清除列表,我似乎有建议使用的例子,
myList = new List<myType>();
而不是
myList.Clear();
我能想到的一些原因为什么我更喜欢其中一个而不是另一个,但还有其他吗关于何时一种方法优于另一种方法的良好基准或指南?
Given a list:
List<myType> myList = new List<myType>();
with the collection contents modified at runtime....
To clear the list, I have seem examples which suggest using
myList = new List<myType>();
rather than
myList.Clear();
I can think of a number of reasons why I would prefer one over the other but are there any other good benchmarks or guidelines as to when one method is preferable to the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果列表很大(80 KB 或更多),那么它将存储在 大对象堆。 LOH 的官方指导是尽可能重用那里的对象,以减少堆碎片。 LOH 不像常规堆那样被压缩。
对于较小的列表,我发现创建一个新列表总体上通常比调用
Clear
更快。这并不总是如此,因此您最好在应用程序中对这两种方式进行测试。如果您调用
Clear
,它只会将列表中的所有项目设置为其默认值,并将Count
设置为0。它不会更改列表的容量。因此调用 Clear 不会改变分配给集合的内存量。如果您想清除列表并减小其大小,请调用Clear
,然后调用TrimExcess
。如果你不小心的话,你会遇到的一个问题就是别名。如果您有多个对象引用同一列表,则创建新列表不会删除这些其他引用。那么你最终会得到两个列表。只是需要思考一下。
总而言之,我认为没有特定的“最佳实践”。我发现有时使用
Clear
很好,有时最好分配一个新列表。If a list is large (80 kilobytes or more), then it's going to be stored on the large object heap. The official guidance for the LOH is to re-use objects there as much as possible in order to reduce heap fragmentation. The LOH isn't compacted like the regular heap is.
For smaller lists, I've found that it's often faster overall to create a new one than it is to call
Clear
. This isn't always true, so you're probably best off testing it both ways in your applications.If you call
Clear
, it just sets all of the items in the list to their default values and setsCount
to 0. It does not change the list's capacity. So callingClear
will not change the amount of memory allocated to the collection. If you want to clear the list and reduce its size, callClear
and thenTrimExcess
.One problem you'll run into if you're not careful is aliasing. If you have multiple objects that refer to the same list, creating a new list doesn't remove those other references. So then you end up with two lists. Just something to think about.
All told, I don't think there's a particular "best practice" for this. I've found that sometimes it's good to use
Clear
and sometimes it's best to allocate a new list.当您绑定到列表对象并从其他代码区域引用它时,请使用clear方法。如果您没有引用并且未绑定,那么创建一个新对象将是合适的。
When you are binding to the list object and have references to it from other areas of code - then use the clear method. If you have no references and are not bound then creating a new object would be appropriate.
使用 Clear 时要小心。它修改实际列表。如果您在某个地方有另一个对同一列表的引用,也许以不同的名称,您可能会得到意想不到的结果。
例如:
因此,为了避免出现意想不到的后果,我宁愿按照惯例制作一个新的 List。 Clear 可以在某些情况下使用,只需确保您确实想要清除它所清除的所有内容即可。
Be careful using Clear. It modifies the actual list. If you have another reference to that same list somewhere, perhaps under a different name, you may have unexpected results.
For example:
Therefore, in order to avoid unintended consequences, I prefer to make a new List as the usual practice. Clear can be used in certain cases, just make sure you really want to clear everything that it clears.