Maybe to late to mention but didnt found others mentioned it: I noticed the += operator in your question. Looks like you are creating some hex output of something executing this operation in cycle.
Using concat on strings (+=) especially in cycles may result in hardly foundable problem: an OutOfMemoryException while analysing the dump will show tons of free memory inside!
What happens?
- The memory management will look for a continous space enough for the result string.
- The concatenated string written there.
- The space used for storing value for original left hand side variable freed.
Note that the space allocated in step #1 is certainly bigger than the space freed in step #3.
In the next cycle the same happens and so on.
How our memory will look like assuming 10 bytes long string was added in each cycle to an originally 20 bytes long string 3 times?
[20 bytes free]X1[30 bytes free]X2[40 bytes free]X2[50 bytes allocated]
(Because almost sure there are other commands using memory during the cycle I placed
the Xn-s to demonstrate their memory allocations. These may be freed or still allocated, follow me.)
If at the next allocation MM founds no enough big continous memory (60 bytes)
then it tries to get it from OS or by restructuring free spaces in its outlet.
The X1 and X2 will be moved somewhere (if possible) and a 20+30+40 continous block become available. Time taking but available.
BUT if the block sizes reach 88kb (google for it why 88kb) they will be allocated on
Large Object Heap. Free blocks here wont be compacted anymore.
So if your string += operation results are going past this size (e.g. you are building a CSV file or rendering something in memory this way) the above cycle will result in chunks of free memory of continously growing sizes, the sum of them can be gigabytes, but your app will terminate with OOM because it wont be able to allocate a block of maybe as small as 1Mb because none of the chunks are big enough for it :)
Sorry for long explanation but it happened some years ago and it was a hard lession. I am fighting against unappropriate use of string concats since then.