本文介绍了大对象堆压缩,什么时候好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,多大才算大?无论如何确定一个对象在堆中有多大?

First off, how big is considered large? Is there anyway to determine how large an object is in heap?

.Net 4.5.1 带有这个 LargeObjectHeapCompactionMode:

.Net 4.5.1 comes with this LargeObjectHeapCompactionMode:

LargeObjectHeapCompactionMode 属性设置为GCLargeObjectHeapCompactionMode.CompactOnce,下一个全阻塞垃圾收集(和 LOH 的压缩)发生在未来时间不确定.您可以通过以下方式立即压缩 LOH使用如下代码:

GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
GC.Collect();

据我所知,压缩 LOH 是一件坏事!那么,哪一个最差呢?紧凑的 LOH 还是有 LOH 碎片?

From what I've heard, it's a bad thing to compact LOH! So, which one is worst? Compact LOH or having LOH fragmentation?

推荐答案

Allocations >= 85 KB 进入 LOH.压缩 LOH 也不错——只是 LOH 碎片不是大多数应用程序需要担心的问题,因此对他们来说,压缩成本不值得.

Allocations >= 85 KB go onto the LOH. Compacting the LOH is not bad -- it's just that LOH fragmentation isn't something the great majority of apps need to worry about, so for them it's not worth the expense of compacting.

当您分配几个大对象并且它们都从地址空间的同一页中取出,然后让其中的一些对象被收集时,就会发生碎片.该页面中剩余的可用空间可能无法使用,因为它太小,或者甚至只是被遗忘",因为分配器永远不会重新考虑再次使用它.

Fragmentation occurs when you allocate several large objects and they all get taken from the same page of address space, then let some of those objects get collected. The remaining free space in that page might be unusable because it is too small, or even simply "forgotten" in the sense that the allocator won't ever reconsider using it again.

最终可以使用的干净页面越来越少,因此分配器将开始变慢,因为它强制移动对象甚至开始抛出 OutOfMemory 异常.压缩将这些对象移动到新页面,回收可用空间.

Eventually there are fewer and fewer clean pages to use, so the allocator will start to slow down as it forcibly moves objects or even start throwing OutOfMemory exceptions. Compaction moves those objects to new pages, reclaiming that free space.

您的应用是否有这种对象使用模式?大多数没有.而在 64 位平台上,您甚至可能不会注意到它,因为在它成为一个大问题之前,有更多的地址空间需要碎片化.

Does your app have this object usage pattern? Most don't. And on 64-bit platforms, you might not even notice it as there's quite a bit more address space to fragment before it becomes a huge issue.

这篇关于大对象堆压缩,什么时候好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 02:48