在循环内部延迟可变变量会导致内存泄漏吗? [英] deferring varirable inside the loop can cuase memory leak?

查看:76
本文介绍了在循环内部延迟可变变量会导致内存泄漏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道如果创建对象实例是for循环或foreach,将如何分配内存(在堆栈和堆中).只是想知道我们是否实例化for循环内的对象会导致内存泄漏,因为我们正在递减该变量.

Just want to know how the memory will be allocated( In stack and heap) if create the instanace of an object inisde the for loop or foreach. Just want to know if we instantiate the objects inside for loop can casue the memory leak or not, because we are dereffering the variable.

Clas test1
{
}
class Ravi
{
public void Foo()
{
for(i=0;i<1000;i++)
{
test 1= new test1();
}
}
}

推荐答案

循环内对象的实例化是完全有效的.如果您这样做的话,就不要指望有任何泄漏.

但是,请参阅我对这个问题的评论.另外,注释中的代码可能有些道理,但是几乎没有这个问题本身的代码.这是因为变量不能使用名称"1",并且因为在每个循环中都使对象无法访问,所以此代码的唯一有用作用可能是构造函数本身的副作用,但这实际上是不好的编程样式.您应该对变量做些事情.同样,使用Console.WriteLine的代码示例也可以理解.请从注释中删除它,并使用改进问题"替换问题正文中的代码.

现在,我不想造成错误的印象,即不可能泄漏托管内存.想象一下,您将在循环中创建的test类型的值添加到某个集合中.想象一下,带有循环的方法被重复调用,并且您永远不会从集合中删除项目.通过某些(可访问的)集合保持对创建对象的引用可访问的事实,可以防止这些对象被垃圾回收,从而有效地产生了无限量的内存泄漏.

有两种方法可以解决此问题.首先,要确保在应用程序的生命周期中从不再使用的对象中正确清理此类集合.第二种方法是使用弱引用.您将需要在此处阅读有关弱引用及其用法的信息:
http://msdn.microsoft.com/en-us/library/system.weakreference.aspx [ ^ ].

弱引用会保留对对象的访问权限,而不会阻止对其进行垃圾回收.要测试某个对象是否被垃圾回收,可以使用属性IsAlive:
http://msdn.microsoft.com/en-us/library/system.weakreference. isalive.aspx [ ^ ].

要了解垃圾收集工作原理的原理,您应该了解称为 reachability 的关键概念是如何工作的.这件事并不像人们想象的那么琐碎.例如,如果您有三个循环引用的对象,会发生什么情况:A引用B,B引用C和C引用A.是否会对其进行垃圾回收?答案是:在没有其他参考文献的情况下,他们会的.垃圾收集器可以检测到这些孤立的内存孤岛"并进行收集.

可到达性和垃圾回收在这里得到了很好的解释:
http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29 [ ^ ].



另请参阅我过去有关相关主题的答案:
垃圾回收负责所有内存管理 [ ^ ].

—SA
Instantiation of the object inside loop is perfectly valid. You should not expect any leak if you do just that.

But please see my comment to the question. Also, your code in the comment could make some sense, but hardly the code in the body of this question itself. This is because the variable cannot have the name "1" and because you leave the object inaccessible in every cycle, the only useful effect of this code could be the side effect of the constructor itself, but it would be really bad programming style. You should do something with your variable. Again the code sample with Console.WriteLine can make sense. Please remove it from the comment and replace the code in the body of the question using "Improve question".

Now, I don''t want to create false impression that the leak of managed memory is impossible. Imagine that you add the values of the type test created in the loop to some collection. Imagine that your method with the loop is called repeatedly and you never remove items from the collection. The fact that the referenced to the created objects are kept accessible through some (accessible) collection prevents those objects from being garbage-collected, effectively creating an unlimited amount of memory leak.

There are two approaches to this problem. First is making sure that the collections of this kind are properly cleaned during the life cycle of the application from the object that will not be used any longer. The second approach is using weak references. You will need to read about weak referenced and their usage here:
http://msdn.microsoft.com/en-us/library/system.weakreference.aspx[^].

Weak references keep access to the objects without preventing them from being garbage-collected. To test if some object was garbage-collected or not, you can use the property IsAlive:
http://msdn.microsoft.com/en-us/library/system.weakreference.isalive.aspx[^].

To understand why garbage collection works as it works, you should understand how the key concept called reachability works. This thing is not so trivial as one might think. For example, what happened if you have three circularly referenced objects: A references B, B references C and C references A. Will they be garbage-collected? The answer is: in the absence of other references, they will. The Garbage Collector can detect such isolated "islands" of memory and collect them.

Reachability and Garbage Collection are well explained here:
http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].



See also my past answer on a related topic:
Garbage collectotion takes care of all the memory management[^].

—SA


这篇关于在循环内部延迟可变变量会导致内存泄漏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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