.net 中列表中的 Trimexcess
用所需的字符串填充列表后,如果不再进一步追加列表,是否应该调用trimexcess?
After filling a list with the the needed strings, if the list won't be further appended, should trimexcess be called?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
加载列表后调用
TrimExcess
可能会节省一些内存。但请记住,如果列表中的项目属于引用类型(并且字符串是引用类型),那么您所节省的只是保存引用所需的内存。因此,举例来说,如果您有一个分配了 2,000 个项目的
List(of String)
,但其中只有 1,000 个项目,则调用TrimExcess
将为您节省1,000 个引用占用的内存。这在 32 位运行时为 4,000 字节,在 64 位运行时为 8,000 字节。正如 Andrew Hare 提到的,在加载将在内存中停留一段时间的列表后调用
TrimExcess
可能是一件好事。如果您从列表中删除了一大堆内容,那么您也可以考虑调用TrimExcess
,然后您将保留该列表。但是对同一个列表重复调用TrimExcess
,除非它真的变得很大,否则只是浪费时间。Calling
TrimExcess
after you load the list will likely save you some memory. But remember that if the items in your list are of a reference type (and strings are reference types), then all you're saving is the memory required to hold the references.So, for example, if you have a
List(of String)
that's allocated for 2,000 items and there are only 1,000 items in it, callingTrimExcess
is going to save you the memory occupied by 1,000 references. That's 4,000 bytes on the 32-bit runtime, and 8,000 bytes on the 64-bit runtime.As Andrew Hare mentioned, calling
TrimExcess
after loading a list that's going to hang around in memory for a while is probably a good thing. You might also consider callingTrimExcess
if you remove a whole bunch of things from a list, and then you're going to keep the list around. But callingTrimExcess
repeatedly for the same list, unless it's really getting large, is just wasting time.当然可以,但请记住:
我不会太担心这个问题,除非您发现大量分配和稀疏填充的列表占用了太多内存。
You certainly can, but keep in mind:
I wouldn't worry too much about this unless you have found that large-allocated and sparsely-filled lists are taking up too much memory.