You should set maxchars in the inspector to what you'd think is a sensible maximum in the inspector. This allows you to avoid memory allocation at runtime. If you don't care about that though, then you can simply set textMesh.maxChars to your max string length.
The code tries hard to avoid memory allocations, and with a little care you can avoid runtime allocations altogether.