如何处置 TextBuilder?
出于纯粹的好奇,除了明显的 MyBuilder = New StringBuilder
和 MyBuilder.Remove(0,长度)
(虽然我猜后者不会释放任何东西,不是吗?)
谢谢!
金融理财师。
Out of pure curiosity, is there a way to free the memory used by a StringBuilder
, other than the obvious MyBuilder = New StringBuilder
and MyBuilder.Remove(0, Length)
(although I guess the later wouldn't free anything, would it?)
Thanks!
CFP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最好的方法是让它超出范围,GC 将处理内存。
The best way is to leave it fall out of scope and the GC will take care of the memory.
你是对的,使用Remove()方法只是重置内部索引,它不会对内部数组做任何事情。不显式管理内存是 .NET 框架的一个关键属性,垃圾收集器会自动对其进行排序。
从技术上讲,您可以通过调用 GC.Collect() 来实现。然而,这几乎总是一个非常糟糕的主意。是的,您将使 StringBuilder 实例及其内部数组消失,前提是没有留下任何引用。但它也会过早地提升程序中的其他对象。这意味着它们的停留时间会超过必要的时间,您的程序最终将使用更多内存。
You are right, using the Remove() method just resets an internal index, it doesn't do anything with the internal array. Not explicitly managing memory is a key property of the .NET framework, the garbage collector sorts it out automatically.
Technically you can by calling GC.Collect(). That is however almost always a Really Bad Idea. Yes, you'll make the StringBuilder instance and its internal array disappear, providing there are no references left to it. But it also promotes other objects in your program too early. Which means they'll stick around longer than necessary, your program will eventually use more memory.
您可以让它超出范围或删除(0,长度),然后将容量设置为0(我认为如果将容量设置为0,它会释放额外的内存,但我不完全确定)。
You can either let it fall out of scope or Remove(0, Length) and then set Capacity to 0 (I think it frees the extra memory if you set Capacity to 0, but I am not entirely sure).
您始终可以使用
MyBuilder = Nothing
,但我通常只是让它们超出范围。You could always use
MyBuilder = Nothing
, but I usually just let them fall out of scope.为 .NET 中的任何对象“释放内存”的方法是让 GC 来处理它。
对于
StringBuilder
,只需将实例设置为Nothing
* 即可。我认为您可能对 .NET 中“处置”对象的含义感到困惑。 IDisposable 接口的目的是提供一种机制,对象可以通过该机制释放对某些共享资源(例如文件流)的访问。调用 Dispose 与释放内存不同。由于
StringBuilder
不访问任何共享资源,因此它不需要实现IDisposable
。如果您必须强制释放内存,则可以使用
GC.Collect
用于。但老实说,我从未遇到过自己调用它有意义的场景(而不是让 GC 决定何时有意义执行收集)。*我假设它是一个类级变量。如果它一开始只是一个局部变量,则无需将其设置为
Nothing
,因为它很快就会超出范围。The way you "free memory" for any object in .NET is to let the GC take care of it.
In the case of a
StringBuilder
, just set the instance toNothing
* and let it be.I think you might be confused about what it means to "dispose" of objects in .NET. The purpose of the
IDisposable
interface is to provide a mechanism by which objects can release access to some shared resource, e.g., file streams. CallingDispose
is not the same as releasing memory. Since aStringBuilder
does not access any shared resources, it does not need to implementIDisposable
.If you must force memory to be freed, that's what
GC.Collect
is for. But honestly, I've never encountered a scenario where it made sense to call that yourself (as opposed to letting the GC decide when it makes sense to perform a collection).*I am assuming it's a class-level variable. If it was only a local variable to begin with, there's no need to set it to
Nothing
as it will soon fall out of scope anyway.