All, I have the following Append which I am performing when I am producing a single line for a fixed text file
formattedLine.Append(this.reversePadding ?
strData.PadLeft(this.maximumLength) :
strData.PadRight(this.maximumLength));
This particular exception happens on the PadLeft() where this.maximumLength = 1,073,741,823 [a field length of an NVARCHAR(MAX) gathered from SQL Server]. formattedLine = "101102AA-1" at the time of exception so why is this happening. I should have a maximum allowed length of 2,147,483,647?
I am wondering if https://stackoverflow.com/a/1769472/626442 be the answer here – however, I am managing any memory with the appropriate Dispose() calls on any disposable objects and using block where possible.
Note. This fixed text export is being done on a background thread.
Thanks for your time.
Right. So you’re trying to create a string with over a billion characters in.
That’s not going to work, and I very much doubt that it’s what you really want to do.
Note that each
charin .NET is two bytes, and also strings in .NET are null-terminated… and have some other fields beyond the data (the length, for one). That means you’d need at least 2147483652 bytes + object overhead, which pushes you over the 2GB-per-object limit.If you’re running on a 64-bit version of Windows, in .NET 4.5, there’s a special app.config setting of
<gcAllowVeryLargeObjects>that allows arrays bigger than 2GB. However, I don’t believe that will change your particular use case:What would you want to do with such a string after creating it, anyway?