Checking for large object heap fragmentation
Published 17 December 2012
We recommend that you check for large object heap fragmentation before testing for other memory problems.
If you aren't familiar with memory profiling, we recommend that you read the .NET Memory Primer before starting.
The main symptom of large object heap fragmentation is that your application throws OutOfMemory exceptions after you've been using the application for a while, and that the timing of the exception isn't predictable - it doesn't seem to be linked to any particular action.
Information about large object heap fragmentation is not available when you profile a .NET 1.1 application.
To check for large object heap fragmentation
On the Summary, look at the Memory fragmentation section, which gives details about the fragments of memory in your application. This section tries to identify any fragmentation problems.
If this section detects fragmentation, switch to the Class list and apply the filter Objects on the large object heap. This shows you which objects are currently on the large object heap, so you can investigate why they are in memory.
If the large object heap isn't fragmented, we recommend that you check for unmanaged memory usage next.
How ANTS Memory Profiler identifies fragmentation problems
ANTS Memory Profiler uses several measures to identify fragmentation, including the size of the largest fragment compared to the total available free space. For information about how the fragmentation levels are calculated, see Fragmentation notices.
Solving large object heap fragmentation
Large object heap fragmentation can be one of the most difficult types of memory problem to solve, because it often involves changes to the architecture of the application. The best approach to use will depend on the exact nature of your program:
- Split arrays into smaller units so that they remain below 85kB (and so are never allocated on the large object heap).
- Alternatively, you can allocate the largest and longest-living objects first (if your objects are files which are queued for processing, for example).
- If you're using .NET 4.5.1, you can choose to compact the large object heap. This is only during a full blocking garbage collection, which can have a significant performance impact. See GCSettings.LargeObjectHeapCompactionMode Property (MSDN) for more details.
- In some cases, it may be that periodically stopping and restarting the program is the only option.