Wednesday, 23 October 2013

Solution for the memory fragmentation in .Net LOH

I don't really blog here about current technical questions. The reason is that most of the posts would be outdated in a year or two. But this one is a "breaking news" and it's worth a post. Short technical background:
Most developers assume that because of Garbage Collector (GC) they don't have to deal with the memory in .Net. It is true. Most of the time. But it is ALWAYS worth to learn what's going on behind the curtains. And if one digs into the details of GC, they will find at least two possible issues: pinned objects and the fragmentation of the Large Object Heap (LOH). The specialty of LOH is that it is not compacted when GC happens, so it can become fragmented over time and cause OutOfMemoryException even if the numbers say that you are far below your memory limits. Maybe you never experienced this. And maybe you didn't because IIS regularly restarts your processes. Another "dive deeper" topic. Anyway. .Net 4.5.1 has a solution. Or more precisely: it gives you the power to address the LOH fragmentation. It is not automatic. You have to deal with it. But now, at least you can.