Bill Robertson's Blog

NYC January Code Camp and the Large Object Heap

I presented my Garbage Collection/Memory Management talk this last weekend at the NYC Code Camp at the Microsoft Office.  I had a great time and it was enjoyable catching up with old friends and meeting new people.

To the gentlemen who was asking about the Large Object Heap and DataSets.  A DataSet will not be placed on the LOH.  A value of a DataColumn instance might be placed there, but the DataSet itself has a footprint much smaller than the 85 K size required to be placed there.

The Large Object Heap is a heap structure used by .net to allocate large objects greater than 85K.  It is not the size of the object graph that places it on the large object heap.  Translated to this example, the memory allocation of the DataSet is NOT the size of all the rows and all the columns.  The size of the object is determined immediately before the constructor executes (before the DataSet has any rows).  This is determine purely by the size of all the fields in the object, and some additional object overhead.

When the memory size is determined and is (currently) above the threshold of 85K, it is placed in the Large Object Heap.  The LOH is very similar to the Small Object Heap in behavior with the ptrNextObj and serial allocation.  (The one covered in the session).  However, it does not have generational support and most importantly the LOH is never compacted.

The lack of compaction is what will create out of memory exceptions because the LOH becomes fragmented and with the new allocations always appearing at the end of the heap, you can run out of memory.

Using the new 2.0 MemoryFailPoint object you can test the allocation and create an InsufficientMemoryException rather than the more fatal OutOfMemoryException. 

This is only a tease because you still can't force a compaction of the LOH.  It is valid, in some scenarios, force a GC.Collect to help with the SMO and free up some memory.  However, the SMO is usually handled quite well by the CLR's collector and calling GC.Collect is nothing more than Jazz Hands to make you feel important.  Don't call it, ever...unless you have a good reason...but never call it.

Leave a Comment

(required) 

(required) 

(optional)

(required)