垃圾收集器是否处理循环内定义的对象的实例? [英] Does Garbage Collector disposes instance of an object defined within a loop?

查看:65
本文介绍了垃圾收集器是否处理循环内定义的对象的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
如果我有类似以下代码的内容:

Hi all,
If I have something like the code below:

while(true)
{
BlaBla MyInstance = new BlaBla();

//do something with this instance...
}



GC是否会处理循环先前迭代的"MyInstance",否则会发生内存崩溃?



Will the GC dispose "MyInstance" of previous iterations of the loop, or there will be a memory crash?

推荐答案

别担心,GC会照顾好它.规则是:当引用变得不可访问时,将迟早收集对象.以这种方式不可能导致内存泄漏.

但是,即使没有非托管代码,.NET中的内存泄漏也很可能发生,但是这些泄漏是设计使然.例如,您有一个辅助容器用于加速搜索或某些操作(例如字典),而在清理主容器或包含控件的数据时忘记清理它.

—SA
Don''t worry, GC takes care about it. The rule is: when the reference becomes inaccessible, the object will be collected... sooner or later. It is not possible to cause a memory leak in this way.

Nevertheless, memory leaks in .NET are quite possible even without unmanaged code, but those leaks are by design. For example, you you have an auxillary container used to accelerate search or something (like dictionary) and forget clean it when you clean your main container or data containing control.

—SA


将在每次迭代开始时创建一个新实例.将新实例分配给变量后,旧实例将可用于垃圾回收.有点像这种情况:
A new instance will be created at the beginning of each iteration. The old instance will be available for garbage collection once the new instance is assigned to the variable. It''s a bit like this scenario:
// First instance.
BlaBla MyInstance = new BlaBla("First Instance");
// As soon as this is assigned, the first instance will be available for garbage collection.
MyInstance = new BlaBla("Second Instance");
// As soon as this is assigned, the second instance will be available for garbage collection.
MyInstance = new BlaBla("Third Instance");
// As soon as this is assigned, the third instance will be available for garbage collection.
MyInstance = new BlaBla("Fourth Instance");
MyInstance = null;
// The fourth instance is now available for garbage collection.


每次新分配都会使以前的实例不再被引用.一旦不再引用实例,就可以将其用于垃圾回收.基本上,您不应该遇到任何内存问题.


Each new assignment makes the previous instance no longer referenced. Once an instance is no longer referenced, it is available for garbage collection. Basically, you shouldn''t come across any memory problems.


我认为您可能会对此处得到的答案感到困惑,因为评论中有些争论. SA是正确的,垃圾回收和Dispose模式是两个不同的东西.也就是说,如果您在循环中执行此操作,并且您的对象是具有非平凡的Dispose-requirements的IDisposable 类型,则绝对必须在循环内调用Dispose (或使用using -block).再次独立于GC,但这是您需要做的事情.

现在,如果您的类不是IDisposable 而是涉及非平凡初始化的类型,则重新设计该类,以便您可以只创建一个实例,然后重用它,这可能是个好主意.例如,如果要循环添加到文件,则在每次迭代中打开文件,写入文件和关闭文件都没有任何意义.在循环外将其打开一次,在循环中进行写操作,并在退出循环后将其关闭.我只是以该示例为例,您的情况可能并不相似.

但是,仅担心内存堆积这一事实就发出信号,表明您正在创建的类可能不是轻量型,这就是为什么我认为我会投入2美分.
I think you may be a tad confused with the answers you got here specially since there''s been some arguing in the comments. SA is right, garbage collection and the Dispose pattern are two different things. That said, if you are doing this in a loop, and your object is an IDisposable type with non trivial dispose-requirements, you absolute must call Dispose (or use the using-block) inside the loop. Once again this is independent of GC but something you need to do.

Now, if your class is not an IDisposable but is a type that involves non-trivial initialization, it may be a good idea to redesign the class so that you can create just a single instance, and then reuse it. As an example, if you are appending to a file in a loop, it does not make any sense to open the file, write to it, and close it in each iteration. Open it once outside the loop, do the writes in the loop and close it once you exit the loop. I just used that as an example, your scenario may not be similar.

But the mere fact that you were concerned about memory piling up set off a signal that perhaps the class you are creating may not be a light type, which is why I thought I''d throw in my 2 cents.


这篇关于垃圾收集器是否处理循环内定义的对象的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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