.netstringlarge-object-heapmemory-fragmentation

how to avoid strings causing fragmentation into LOH


My application read lot of data from text files that get into large object heap which eventually cause fragmentation issue. Are there any alternative design approaches that can help to not get these to LOH? I know about StringBuilder but it seem like reading from text file still internally create large strings into LOH.


Solution

  • If you don't want to change the calls to ReadAllLines there is no way to avoid allocations to the LOH when strings are large enough (short of cloning the .Net github repo, changing the allocation strategy, and recompiling it). Any other solution would see either reading smaller strings from the file, or reading the file as bytes into your own buffer where you then manage allocation and arrangement of the bytes on your own, likely augmented by creating your own string class (since System.String always copies into its own buffer).

    However you do now have a choice with .Net >=4.5.1: LOH compaction. To perform this manually, execute the following:

    GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
    GC.Collect();