是内存泄漏可能在托管环境,如.NET? [英] Are memory leaks possible in managed environments like .NET?

查看:164
本文介绍了是内存泄漏可能在托管环境,如.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中它很容易可以有一个永久的内存泄漏 - 只是分配内存,不释放它:

In C++ it is easily possible to have a permanent memory leak - just allocate memory and don't release it:

new char; //permanent memory leak guaranteed

和该存储器保持分配给堆(通常相同程序运行时间的持续时间)的寿命。

and that memory stays allocated for the lifetime of the heap (usually the same as program runtime duration).

时相同的可能在C#程序(这将导致一个特定的未引用的对象永远不会被释放,而内存管理机制,正常工作情况下)?

Is the same (a case that will lead to a specific unreferenced object never been released while memory management mechanisms are working properly) possible in a C# program?

我已经仔细阅读这个问题和答案给它,它提到某些情况下,这导致越来越内存消耗较高比预期或IMO比较极端的情况下,像死锁终结器线程,但可以永久的泄漏可以形成一个C#程序与正常工作的内存管理?

I've carefully read this question and answers to it and it mentions some cases which lead to getting higher memory consumption than expected or IMO rather extreme cases like deadlocking the finalizer thread, but can a permanent leak be formed in a C# program with normally functioning memory management?

推荐答案

这要看你如何定义内存泄漏。在一个非托管的语言,我们通常想到的内存泄漏作为的情况下内存的分配,并没有对它的引用存在,所以我们的无法的释放它。

It depends on how you define a memory leak. In an unmanaged language, we typically think of a memory leak as a situation where memory has been allocated, and no references to it exist, so we are unable to free it.

这是一种泄漏是pretty的多少不可能建立在.NET(除非你召唤出非托管code,或除非有在运行时的错误)。

That kind of leaks are pretty much impossible to create in .NET (unless you call out into unmanaged code, or unless there's a bug in the runtime).

但是,可以得到另一个弱的形式泄漏的:当一个对存储器的确实的存在(因此它仍然是可能找到并重置基准,使由GC以释放内存正常),但你的认为的没有,所以你的假设的对象被引用会得到GC'ed。这很容易导致无限增长的内存消耗,因为你是堆放引用那些不再使用的对象,但不能被垃圾收集,因为他们仍然在你的应用程序的某个地方引用。

However, you can get another "weaker" form of leaks: when a reference to the memory does exist (so it is still possible to find and reset the reference, allowing the GC to free the memory normally), but you thought it didn't, so you assumed the object being referenced would get GC'ed. That can easily lead to unbounded growth in memory consumption, as you're piling up references to objects that are no longer used, but which can't be garbage collected because they're still referenced somewhere in your app.

那么,什么是通常被认为在.NET中的内存泄漏是一个简单的情况下,你的忘记的,你有一个参考的对象(例如,因为你没有从事件退订)。但引用是否存在,如果你还记得的话,你可以清除它的泄漏会自行消失。

So what is typically considered a memory leak in .NET is simply a situation where you forgot that you have a reference to an object (for example because you failed to unsubscribe from an event). But the reference exists, and if you remember about it, you can clear it and the leak will go away.

这篇关于是内存泄漏可能在托管环境,如.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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