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 are not familiar with memory profiling, we recommend that before starting you read the .NET Memory Primer.
The main indicator of large object heap fragmentation is that your program throws OutOfMemory exceptions after you have been using the application for a while, and that the timing of the exception's occurrence is not predictable.
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. This shows the amount of fragmentation on the large object heap. In addition, ANTS Memory Profiler heuristically checks whether fragmentation could be causing problems for your application.
If ANTS Memory Profiler detects fragmentation, apply the filter Objects on the large object heap. This can be useful, for example, to find out which objects are currently on the large object heap, and why they are in memory.
If the large object heap is not fragmented, we recommend that you check for unmanaged memory usage next.
How ANTS Memory Profiler identifies fragmentation problems
ANTS Memory Profiler compares the size of the largest fragment to the total available free space, and also checks how much allocated memory is unused. For a description of the different notices displayed by ANTS Memory Profiler if fragmentation is detected, see Fragmentation notices in ANTS Memory Profiler 7.
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).
- In some cases, it may be that periodically stopping and restarting the program is the only option.