The Trim is part of the code that munges email headers. So if it’s not Trim that is leaking then the whole class would be problematic. I’ll try replacing the Trim with a Regex first.
Why is it the String that is too large?
I suspect it’s more the fact that a lot of Strings are being instantiated in that loop.
And memory management might kick in too late (*).
This works just fine in a 32Bit app:
Dim s As String = " blabla "
Dim sa() As String
For i As Integer = 0 To 10000
sa.Append(s)
Next
s = Join(sa, "")
(*) I’d actually like to read an explanation about what’s really happening with the other code. Why that leads to a crash which I suspect is an “Out of Memory” situation.
Beatrix’s loop would cause the string length to exceed the size of a UInt64 very quickly because it is doubling the size of the string on each iteration.
The reason your code works is that it is creating 10,000 strings which is not the same as what Beatrix’s code does.