19

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

.Net 4.5.1 comes with this LargeObjectHeapCompactionMode:

After the LargeObjectHeapCompactionMode property is set to GCLargeObjectHeapCompactionMode.CompactOnce, the next full blocking garbage collection (and compaction of the LOH) occurs at an indeterminate future time. You can compact the LOH immediately by using code like the following:

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

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

3
  • Don't worry about the GC unless you really have a problem. Manually fiddling with the GC hardly ever brings any advantage.
    – PMF
    Nov 17, 2013 at 20:53
  • @HansPassant hahaha...performance wise, for non-cranky programmers, is it really that bad? Nov 17, 2013 at 20:55
  • 1
    It's not bad or good; it's a trade-off. If you have an app running into OOM due to LOH fragmentation it may be better in some cases to compact the LOH and squeeze an extra bit of run-time out of your app. The other option (and, prior to .NET 4.5, the only option) is to restart the app when the LOH becomes too fragmented. You will need to profile your application with and without LOH compaction to determine the performance impact. (I suppose another option is to avoid creating large objects, for example if you deal with large arrays you can segment them, but.. ugh). Apr 3, 2014 at 20:52

1 Answer 1

17

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.

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.

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.

1
  • 4
    Excellent answer. I have seen OutOfMemoryException appear frequently on ASP.NET web apps that don't recycle often (or ever) due to LOH fragmentation (verified with WinDbg). (This occurs sooner in 32-bit apps, of course.) For example, ASP.NET apps with large/complex forms (and that use Viewstate) often generate ViewState strings that exceed 85 KB. In the past my solution was to set a recycle schedule and code apps in such a way that recycling the app pool was not noticeable to the user (did not kill their session in such a way that they lost work, for example). Finally, a LOH compact feature! Apr 3, 2014 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.