为什么在运行时间内增加了内存使用量 [英] Why the memory usage is increased, during the running time

查看:96
本文介绍了为什么在运行时间内增加了内存使用量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public  Form1()
{
InitializeComponent();
}

private void button1_Click( object sender,EventArgs e)
{
while true
{
使用(Form2 X = new Form2 ())
{
X.Show();
}
}
}

解决方案

因为你一直在实例化一个新的Form2实例循环时永无止境。



你学会了如何调试吗?它可以让你很快发现问题。


我的5美分:



这里解释垃圾收集很好: https://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29 [ ^ ]。



正如您所看到的,这一切都基于无法访问这一概念,这一点并非无足轻重: https: //en.wikipedia.org/wiki/Unreachable_memory [ ^ ]。



说,取3个对象:引用B,B引用C,C引用A.如果丢失了对所有引用的任何其他引用,将他们被垃圾收集?他们互相拥抱。是! GC足以解决所有这些问题。



现在,这一切都与 IDisposable.Dispose 无关可以通过任何类型实现,以处理一些名为 explicitely 的清理操作(回收非托管资源只是可能的应用程序之一,它可以是任何东西;例如,将鼠标光标设置为默认值glyph),与您从未在应用程序代码中调用的构造函数形成对比。通常,在托管代码中很少使用构造函数,因为GC可以在没有它们的情况下处理大多数情况,并且因为构造函数调用的时刻不能由应用程序控制。因此,构造函数可以在我不关心何时的情况下调用。



至于使用 声明(不要与使用指令混淆),这只是语法糖调用 IDisposable.Dispose ,非常方便。



请参阅:

< a href =https://msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.110%29.aspx> https://msdn.microsoft.com/en-us/library /system.idisposable%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/yh598w02.aspx [ ^ ]。



-SA

public Form1()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    while (true)
    {
        using (Form2 X = new Form2())
        {
            X.Show();
        }
    }
}

解决方案

Because you are constantly instantiating a new Form2 instance in a never-ending while loop.

Did you learn how to debug? It would have allowed you to spot the problem very quickly.


My 5 cents:

Garbage collection is explained very well here: https://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].

As you can see, it all is based on the concept "unreachable", which is not trivial at all: https://en.wikipedia.org/wiki/Unreachable_memory[^].

Say, take 3 objects: A references B, B references C, C references A. If you lose any other references to all of them, will they be garbage collected? They do hold each other. Yes! GC is intellectual enough to resolve all such situations.

Now, it all is unrelated to IDisposable.Dispose which can be implemented by any type, to handle some clean-up actions called explicitely (reclaiming of unmanaged resources is only one of possible applications, it can be anything; for example, setting a mouse cursor to its default glyph), in contrast to constructors which you never call in the application code. Generally, there is a very little use of constructors in managed code, because GC can cope with most situations without them, and because the moment of time of the constructor call is not controllable by the application. So, constructors can be called in "I don't care when" situations.

As to "using" statement (not to be mixed up with with "using" directive), this is nothing but syntactic sugar for calling IDisposable.Dispose, very convenient though.

Please see:
https://msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/yh598w02.aspx[^].

—SA


这篇关于为什么在运行时间内增加了内存使用量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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