想要了解像 C# 这样的托管语言如何存在内存泄漏
由于 C# 是一种托管语言,可以自动执行垃圾收集以清理对象等,...
有哪些方式会导致内存泄漏?
是否有一些不明显的方法值得人们注意?
如何检测或查找内存泄漏(一旦您了解它们是如何生成的等)
Since C# is a managed language that performs garbage collection automatically to clean up objects etc, ...
what are the ways one can introduce a memory leak?
Are there some non-obvious ways that one should look out for?
How can you detect or look for memory leaks (once you understand how they are generated etc.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通常,泄漏以开发人员编写的代码的形式出现,这些代码在不应该“保留”对象的情况下“保留”对象,这随后不允许垃圾收集器收集这些对象。
垃圾收集器非常擅长它的功能,但如果您不了解它在做什么,则在程序中引入内存问题的可能性非常高。
我建议阅读 GC 并了解它的工作原理。
这里有一些可以帮助您入门的内容:
http ://www.simple-talk.com/dotnet/.net-framework/understanding-garbage-collection-in-.net/
Usually leaks show up in the form of a developer writing code that "holds on" to objects when they shouldn't be, which subsequently disallows the garbage collector from collecting on those objects.
The garbage collector is pretty good at what it does, but if you don't understand what it's doing, the likelihood of you introducing memory issues into your program is pretty high.
I would suggest reading up on the GC and understanding how it works.
Here's something to get you started:
http://www.simple-talk.com/dotnet/.net-framework/understanding-garbage-collection-in-.net/
二。
Two.
在 GC 语言中可能导致的明显“内存泄漏”可能只是由于在需要后保留对对象的引用而引起的 - 如果您滚动自己的缓存或保留其他全局状态,则这种情况尤其可能发生。
另一种方法是泄漏未处理的非托管资源中的内存,尽管大多数标准库类可能会在析构函数中处理这些资源,因此内存迟早会被回收。
(由于问题的开放性,我将这篇文章标记为 CW。)
The obvious "memory leak" one could cause in a GC-ed language would be caused simply be retaining a reference to an object after it's needed - this is especially likely if you roll your own caching or keep other global state.
Another way would be leaking memory in unmanaged resources that weren't disposed of, although most of the standard library classes will probably dispose of those in destructors so the memory will be reclaimed sooner or later.
(I'm marking the post as CW because of the open-ended nature of the question.)
内存泄漏意味着保留了不再需要的内存对象。在 C# 中,考虑到 GC 会收集未引用的对象,这相当于保留对不需要的对象的引用。
考虑一下范围不正确的声明、无限递归或迭代......
A memory leak means keeping in memory objects you don't need anymore. In C#, considering the GC collects unreferenced objects, it's equivalent to say keeping references to objects you don't need.
Think about uncorrectly scoped declarations, infinite recursion or iteration...