views:

220

answers:

1

I have an asp.net application that serializes data to the client using JSON. Once load reaches a certain level, the app is spending an in-ordinate time in GC and after spending some time with WinDbg/SOS and related tools it appears that there is a substantial amount of LOH fragmentation taking place because the size of the generated JSON is frequently large enough to exceed the 86K boundary of the LOH.

My question 2 fold 1) Has anybody run this specific scenario of JSON generation impacting and causing LOH fragmentation and 2) Does anybody have any suggestions on how to handle it.

+1  A: 

If you are generating a large number of > 85K strings then that's problematic performance wise anyway.

Do you have a need to generate the data fully before sending it to the client? If your JSON library supports writing the data to a Stream/TextWriter and the client response api supports it I would simply chain one to the other entirely side stepping the problem.

If you can't do this you may have to resort to object pooling, which won't work on strings and you will have to resort to char[] or byte[] (A MemoryStream will make this easy) and reuse the backing array. Probably one per thread is sufficient. You may want to deal with the cases where by an uncommon massive response causes the underlying array to balloon and then be kept around.

ShuggyCoUk