想要了解像 C# 这样的托管语言如何存在内存泄漏

发布于 2024-12-08 15:25:46 字数 144 浏览 0 评论 0原文

由于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雄赳赳气昂昂 2024-12-15 15:25:46

通常,泄漏以开发人员编写的代码的形式出现,这些代码在不应该“保留”对象的情况下“保留”对象,这随后不允许垃圾收集器收集这些对象。

垃圾收集器非常擅长它的功能,但如果您不了解它在做什么,则在程序中引入内存问题的可能性非常高。

我建议阅读 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/

李白 2024-12-15 15:25:46

二。

  • 首先,构建引用对象。就像创建订阅表单上的事件的对象一样。表单处于活动状态,因此无法收集,并且事件订阅...使对象保持活动状态。
  • 其次,阻止本机代码中的垃圾收集器。就像 Oracle ODP.NET 驱动程序在某些条件下所做的那样。停止终结器,因此任何需要终结的对象都不会得到它 - 因此永远不会被释放。

Two.

  • First, building up referenced objects. Like creating objects that subscribe to an event on a form. The form is active, so can not be collected, and the event subscriptions... keep the objects alive.
  • Second, block the garbage colelctor in native code. Like the oracle ODP.NET driever does under certain conditions. Stops the finalizer, so any object requiring finalization WILL NOT GET IT - and thus never be released.
阳光下慵懒的猫 2024-12-15 15:25:46

在 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.)

梦醒时光 2024-12-15 15:25:46

内存泄漏意味着保留了不再需要的内存对象。在 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文