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

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

问题描述

忽略不安全code,.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.

  • 是这样写得很好的底层框架,它绝对不具备的内置内存泄漏的可能?
  • 有框架的$ C $内的东西C,它能够自我管理,甚至治愈了自己想成为的内存泄漏?
  • 是答案别的东西,我没有考虑过?

推荐答案

目前已经有一些很好的答案,但我想解决一个额外的点。让我们来看看非常仔细地再次您的具体问题:

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.

  • 是这样写得很好的底层框架,它绝对不具备的内置内存泄漏的可能?
  • 有框架的$ C $内的东西C,它能够自我管理,甚至治愈了自己想成为的内存泄漏?
  • 是答案别的东西,我没有考虑过?

这里的关键是之间的的code和的及其的code区分开来。 .NET框架(和Java,围棋,Python和其他垃圾收集的语言)承诺,如果你依赖的及其的code,的的code不会内存泄漏......传统意​​义上的最少。你可能会发现自己在某些对象像您期望不释放的情况,但这些情况都是从传统的内存泄漏微妙的不同,因为对象仍然在到达你的程序。

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.

您困惑是因为你正确理解,这是不一样的东西,说你创造不可能有一个传统的内存泄漏在所有的程序。有可能仍然是在一个错误的及其的code泄露内存。

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.

所以,现在你要问自己:你愿意信任你的code,或者他们的code?请记住,在这里,他们的code不仅由最初的开发测试(就像你的,对吗?),它也是身经百战的由其他程序员像你十万(也许百万)的日常使用。任何显著内存泄漏问题将是中发现和纠正的第一件事情。再次,我不是说这是不可能的。只是,它通常是一个更好的主意,相信自己的code比是你自己......至少在这方面。

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天全站免登陆