在.NET垃圾收集(代) [英] Garbage collection in .NET (generations)

查看:187
本文介绍了在.NET垃圾收集(代)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了大量的.NET性能文章描述第1代,2代垃圾收集和对象存活的后代。

I have read a lot of .NET performance articles that describe Gen1,Gen2 garbage collection and objects surviving the generations.

为什么对象生存的集合?

Why does objects survives the collection?

什么是寄托?

我如何了解更多关于此?

How do I learn more about this?

推荐答案

一个原因,你必须多代的垃圾收集器,以避免丢失内存碎片。

One reason you have multiple generations in a garbage collector is to avoid losing memory to fragmentation.

每个函数调用可能意味着建立和<打击>删除集合多个对象,所以你的程序的内存堆往往很快片段。这留下的孔是不是很实用。其结果是,你的程序需要定期去碎片,就像一个硬盘。这是一个收集过程中发生的事情的一部分。

Every function call can mean the creation and deletion collection of multiple objects, and so the memory heap for your program tends to fragment very quickly. This leaves holes behind that aren't very usable. The result is that your program needs to be periodically de-fragmented, just like a hard disk. This is part of what happens during a collection.

当一个对象生存的集合,它被移动到更长的寿命产生的理论,如果它存活一个收集它可能会生存他人。因此,后人少周转,不分段之多。这意味着你的节目围绕花费更少的时间整体杂耍的东西清理孔和废物更少的内存。这也是一个改进了传统的存储器管理(malloc的/游离的或新的/删除),其离开它到操作系统来管理任何存储器分段。

When an object survives a collection, it is moved to a longer-lived generation on the theory that if it survived one collection it will probably survive others. Thus the later generations have less turn-over and don't fragment as much. This means your program spends less time overall juggling things around to clean up holes and wastes less memory. This is also an improvement over traditional memory management (malloc/free or new/delete), which left it up to the operating system to manage any memory fragmentation.

的对象集合幸存的原因是因为有一些地方,仍然是范围,并拥有对该对象的引用。有几个方法可以导致这种情况发生,然后忘掉参考,因此有可能泄漏内存管理code。

The reason an object survives collection is because there is something somewhere that is still "in scope" and holds a reference to that object. There are a few ways you can cause this to happen and then forget about the reference, so it is possible to "leak" memory in managed code.

有时候,人们禁不住要叫 GC.Collect的()在努力让垃圾收集清理的东西了。也许他们已经发现他们有一个泄漏,或者认为存储正成为过度分散。你应该抵制那些冲动。虽然在.net垃圾回收是不完美的,是它的非常的不错,而且它几乎肯定要好得多,在清理的内存比你。赔率是,如果一个对象可以并且应该被收集,这将是。记住,调用 GC.Collect的()实际上可以使事情变得更糟,帮助垃圾收集器移动对象到一个更高的产生,从而保持周围更长的时间比正常情况可以。

Sometimes people are tempted to call GC.Collect() in an effort to get the garbage collector to clean something up. Perhaps they've discovered they have a leak, or think memory is becoming over-fragmented. You should resist those urges. While garbage collection in .Net is not perfect, is it very good, and it's almost certainly much better at cleaning up memory than you are. Odds are that if an object can and should be collected, it will be. Remember that calling GC.Collect() can actually make things worse by helping the garbage collector move objects up to a higher generation, and thus keeping them around for longer than they otherwise would be.

相反,如果你怀疑你有一个泄漏,看你自己的code类似的东西可能持有引用了很多其他项目的全局或静态变量。唯一的一次,你应该叫 GC.Collect的()是当你想过这是不提供给垃圾收集程序的性质的信息,这是pretty的罕见的GC知道你所创建的每个引用。

Instead, if you suspect you have a leak, look to your own code for something like a global or static variable that might hold a reference to a lot of other items. The only time you should call GC.Collect() is when you have information about the nature of the program that is not available to the garbage collector, and that's pretty rare as the GC knows every reference you've created.

钉住是当你需要一个对象传递给非托管库。垃圾收集器在内存中移动对象的物理位置,所以你需要销,它在一个地方或使用的非托管库中的指针可能会变得无效。一个固定对象不能被收集,所以你不应该引脚的对象为高于所需的时间。

"Pinning" is for when you need to pass an object to an unmanaged library. The garbage collector can move an object's physical location in memory, and so you need to "pin" it in one place or the pointer used by the unmanaged library could become invalid. A pinned object cannot be collected, and so you shouldn't pin an object for any longer than necessary.

这篇关于在.NET垃圾收集(代)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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