为什么.NET 不会有内存泄漏? [英] Why can .NET not have memory leaks?

查看:28
本文介绍了为什么.NET 不会有内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

忽略不安全的代码,.NET 不会有内存泄漏.我从许多专家那里无休止地阅读了这篇文章,我相信这一点.但是,我不明白为什么会这样.

Ignoring unsafe code, .NET cannot have memory leaks. I've read this endlessly from many experts and I believe it. However, I do not understand why this is so.

据我所知,框架本身是用 C++ 编写的,而 C++ 容易出现内存泄漏.

It is my understanding that the framework itself is written in C++ and C++ is susceptible to memory leaks.

  • 底层框架是否写得这么好,绝对不存在内部内存泄漏的可能性?
  • 框架代码中是否有一些东西可以自我管理甚至修复自己可能发生的内存泄漏?
  • 这个答案是不是我还没有考虑过?

推荐答案

这里已经有一些很好的答案,但我想补充一点.让我们再仔细看看你的具体问题:

There are already some good answers here, but I want to address one additional point. Let's look very carefully again at your specific question:

据我所知,框架本身是用 C++ 编写的,而 C++ 容易出现内存泄漏.

It is my understanding that the framework itself is written in C++ and C++ is susceptible to memory leaks.

  • 底层框架是否写得这么好,绝对不存在内部内存泄漏的可能性?
  • 框架代码中是否有一些东西可以自我管理甚至修复自己可能发生的内存泄漏?
  • 这个答案是不是我还没有考虑过?

这里的关键是区分你的代码和他们的代码..Net 框架(以及 Java、Go、python 和其他垃圾收集语言)承诺,如果您依赖他们的代码,您的代码不会泄漏内存.. 至少在传统意义上.您可能会发现某些对象没有像您预期的那样被释放,但这些情况与传统的内存泄漏略有不同,因为这些对象在您的程序中仍然可以访问.

The key here is to distinguish between your code and their code. The .Net framework (and Java, Go, python, and other garbage-collected languages) promise that if you rely on their code, your code will not leak memory... at least in the traditional sense. You might find yourself in situations where some objects are not freed as you expect, but these cases are subtly different from traditional memory leaks because the objects are still reachable in your program.

您感到困惑,因为您正确理解这与说您创建的任何程序根本不可能有传统内存泄漏完全不同.他们的代码中仍然可能存在一个导致内存泄漏的错误.

You are confused because you correctly understand that this is not the same thing as saying any program you create can't possibly have a traditional memory leak at all. There could still be a bug in their code that leaks memory.

所以现在你必须问自己:你更愿意相信你的代码,还是他们的代码?请记住,他们的代码不仅经过原始开发人员的测试(就像您的开发人员一样,对吧?),而且还经过数千(可能数百万)像您一样的其他程序员的日常使用而久经沙场.任何重大的内存泄漏问题都将是首先确定和纠正的问题.再说一次,我不是说这是不可能的.只是通常信任他们的代码比信任你自己的代码更好......至少在这方面.

So now you have to ask yourself: would you rather trust your code, or their code? Keep in mind here that their code is not only tested by the original developers (just like yours, right?), it's also battle-hardened from daily use by thousands (perhaps millions) of other programmers like yourself. Any significant memory leak issues would be among the first things identified and corrected. Again, I'm not saying it's not possible. It's just that it's generally a better idea to trust their code than it is your own... at least in this respect.

因此这里的正确答案是它是您的第一个建议的变体:

Therefore the correct answer here is that it's a variant of your first suggestion:

底层框架写的这么好,绝对不存在内存泄露的可能吗?

Is the underlying framework so well-written, that it absolutely does not have any possibility of internal memory leaks?

不是没有可能,而是比自己管理要安全得多.我当然不知道框架中有任何已知泄漏.

It's not that there's no possibility, but that it's much safer than managing it yourself. I'm certainly not aware of any known leaks in the framework.

这篇关于为什么.NET 不会有内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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