简体   繁体   English

Memory 泄露在.Net

[英]Memory leak in .Net

I want to know if really memory is getting leaked.我想知道 memory 是否真的被泄露了。 My Scenaio is like:我的场景是这样的:

  1. My.Net application is taking x amount of memory. My.Net 应用程序正在占用 x 数量的 memory。
  2. i opened some dialogs and now it takes x+y amount of memory我打开了一些对话框,现在需要 x+y 数量的 memory
  3. Closed all the recently opened dialogs关闭所有最近打开的对话框
  4. still memory is around x + Y仍然 memory 在 x + Y 左右

Is this a memory leak or could be a situation that garbage collector have not cleared the memory.这是 memory 泄漏还是可能是垃圾收集器没有清除 memory 的情况。

And as events are also considered as references.并且作为事件也被视为参考。 what if the event is present in a dereferenced object?如果事件出现在取消引用的 object 中怎么办? then that event would not be considered as a reference, right?那么该事件不会被视为参考,对吗?

The garbage collector frees only objects that aren't referenced anymore.垃圾收集器只释放不再引用的对象。

It doesn't magically remove all the objects you don't want anymore.它不会神奇地删除您不再需要的所有对象。

Check if you still have references to any of the objects.检查您是否仍有对任何对象的引用。 Remember that events are also considered references.请记住,事件也被视为引用。 (It needs to know which object to go to) (需要知道 object 到 go 到哪个)

Memory is only garbage collected when needed, more technical when the so called Generation 0 is full or when the overall system memory pressure is suficcient to force a garbage collection. Memory 仅在需要时进行垃圾收集,当所谓的第 0 代已满或整个系统 memory 压力足以强制进行垃圾收集时更具技术性。 Memory is not automatically reclaimed when it goes out of scope. Memory 出 scope 时不会自动回收。 More info here and here .更多信息在这里这里

Just for testing, try calling GC.Collect() after closing the dialog (after it is no longer referenced) to force the GC to collect any available memory.只是为了测试,在关闭对话框后(不再引用它之后)尝试调用GC.Collect() ) 以强制 GC 收集任何可用的 memory。

In reality, you should not fiddle with the Garbage Collector, it is heavily tuned for best performance.实际上,您应该摆弄垃圾收集器,它经过大量调整以获得最佳性能。

If you suspect that you are indeed leaking memory, use some kind of memory profiler to analyze your application.如果您怀疑您确实泄漏了 memory,请使用某种 memory 分析器来分析您的应用程序。

Try for example RedGates Memory Profiler , they have a time-based trial.尝试例如RedGates Memory Profiler ,他们有一个基于时间的试验。
Follow this walk-through to get up to speed and learn a bit what to look for and how.按照此演练快速了解要查找的内容和方法。

In.Net once you dont reference an object the object is collected back. In.Net 一旦你不引用 object,object 就会被收集回来。 If you have a live reference then the object is kept alive.如果您有实时参考,则 object 将保持活动状态。 So to find a memory leak you need to do the following因此,要找到 memory 泄漏,您需要执行以下操作

  1. Find the list of objects and the memory being occupied查找对象列表和被占用的 memory
  2. Identify the objects that you doubt should have been freed by now.确定您怀疑现在应该释放的对象。
  3. Find the root that is holding this object.找到持有此 object 的根。
  4. Fix the root.修复根。

You can either do this with some memory profiler or you can use WinDbg+sos.您可以使用一些 memory 分析器来执行此操作,也可以使用 WinDbg+sos。 People feel windbg is hard to use but for these cases windbg+sos is much more easier to use and it is free.人们觉得 windbg 很难使用,但对于这些情况,windbg+sos 更容易使用并且是免费的。 Also you will feel happy after fixing the leak.修复泄漏后,您也会感到高兴。 you will be one among those who always prefers free powerful tools.您将成为那些总是喜欢免费强大工具的人之一。

Have a look at this link.看看这个链接。

http://blogs.msdn.com/b/ricom/archive/2004/12/10/279612.aspx http://blogs.msdn.com/b/ricom/archive/2004/12/10/279612.aspx

http://blogs.msdn.com/b/delay/archive/2009/03/11/where-s-your-leak-at-using-windbg-sos-and-gcroot-to-diagnose-a-net-memory-leak.aspx http://blogs.msdn.com/b/delay/archive/2009/03/11/where-s-your-leak-at-using-windbg-sos-and-gcroot-to-diagnose-a-net-内存泄漏.aspx

How about using a memory profiling tool such as JetBrains dotTrace to profile the memory usage of your application?如何使用 memory 分析工具(例如JetBrains dotTrace )来分析您的应用程序的 memory 使用情况?

It could be a situation that garbage collector have not cleared the memory可能是垃圾收集器没有清除 memory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM