内存泄漏" A&QUOT剖析; [英] Anatomy of a "Memory Leak"

查看:261
本文介绍了内存泄漏" A&QUOT剖析;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET的角度来看:

In .NET perspective:

  • 什么是 内存泄漏
  • 您如何判断是否您的应用程序泄漏?哪些影响?
  • 您如何能prevent内存泄漏?
  • 如果您的应用程序有内存泄漏,它消失在进程退出或被杀?或者,在你的应用程序的内存泄漏,即使过程中完成?
  • 在影响系统上的其他进程
  • 又是怎么回事,通过COM互操作和/或P访问非托管code /调用?
  • What is a Memory Leak?
  • How can you determine whether your application leaks? What are the effects?
  • How can you prevent a memory leak?
  • If your application has memory leak, does it go away when the process exits or is killed? Or do memory leaks in your application affect other processes on the system even after process completion?
  • And what about unmanaged code accessed via COM Interop and/or P/Invoke?

我有一些这些问题的答案我自己,但他们是不完整的。你怎么看?

I have some answers for these questions myself, but they are incomplete. What do you think?

推荐答案

我见过的最好的解释是在免费的 /$c$cbetter_downloads/entry179694.aspx">Foundations。

The best explanation I've seen is in Chapter 7 of the free Foundations of Programming ebook.

基本上,在.NET时引用的对象扎根,因此不能被垃圾收集发生内存泄漏。当你持有到超出预期的范围内引用该发生意外。

Basically, in .NET a memory leak occurs when referenced objects are rooted and thus cannot be garbage collected. This occurs accidentally when you hold on to references beyond the intended scope.

您就会知道你有泄漏,当你开始outofmemoryexceptions或你的内存使用率上升超过你所期望(Perfmon中具有良好的内存计数器)。

You'll know that you have leaks when you start getting outofmemoryexceptions or your memory usage goes up beyond what you'd expect (perfmon has nice memory counters).

了解.NET的内存模型是你最好避免它的方式。特别是,如何理解垃圾收集器的工作原理,以及如何引用工作(同样,我是指你的电子书的第7章)。另外,还要留意常见的陷阱,可能是最常见的事件。如果对象A注册到对象B的事件,则对象A将留下来,直到对象B消失,因为B就掌握着一个参考A.解决的办法是,当你完成注销您的活动。

Understanding .NET's memory model is your best way of avoiding it. Specifically, understanding how the garbage collector works and how references work (again, I refer you to chapter 7 of the ebook). Also, be mindful of common pitfalls, probably the most common being events. If object A registered to an event on object B, then object A will stick around until object B disappears because B holds a reference to A. The solution is to unregister your events when you're done.

当然,好记性的个人资料将让你看到你的对象图,并探讨你的对象的嵌套/引用,看看引用来自哪里,什么根对象负责(的红门蚂蚁资料,JetBrains公司dotTrace, memprofiler 是真是不错的选择,也可以使用纯文本的WinDbg和SOS,但我强烈建议商业/视频产品,除非你是一个真正的大师)。

Of course, a good memory profile will let you see your object graphs and explore the nesting/referencing of your objects to see where references are coming from and what root object is responsible (red-gate ants profile, JetBrains dotTrace, memprofiler are really good choices, or you can use the text-only windbg and sos, but I'd strongly recommend a commercial/visual product unless you're a real guru).

我相信非托管code是受制于unamanged code典型的内存泄漏,但两者之间的共享引用是由垃圾收集管理。难道是错的最后一点。

I believe unmanaged code is subject to typical memory leaks of unamanged code, except that references shared between the two are managed by the garbage collector. Could be wrong about this last point.

这篇关于内存泄漏" A&QUOT剖析;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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