Your memory is probably fragmented from numerous string operations or other operations that create and free small blocks of memory, such as boxing / unpacking.
You will get this exception when the CLR cannot allocate a sufficiently large free block of memory.
I use the "CLR Profiler" and check the memory allocation. If you see numerous white spots (free blocks) and large free blocks, you need to start watching how you select objects.
For example, before assigning one line to another, make sure that the lines are different first. Using StringBuilder is all cases that eliminate boxing and other memory optimizations.
I use this technique and completely eliminate exceptions, with the exception of a known issue with binary de-serialization.
Recover the lost art of memory optimization in managed code http://msdn.microsoft.com/en-us/magazine/cc163856.aspx
Studying memory problems http://msdn.microsoft.com/en-us/magazine/cc163528.aspx
Performance optimization in Visual Basic.NET at http://msdn.microsoft.com/en-us/library/aa289513 (v = vs .71) .aspx
Amissico
source share