I have a large object in my C# code which can be as large as 15GB. Internally it has a 2D array of doubles and 2 lists of strings which describe the rows and columns of the 2D array.
This object has a method WriteToTextWriter
(StreamWriter
s) which writes a header and the entire data in the 2D array to the StreamWriter
s. The StreamWriter
is initialized using a MemoryStream
object.
I have another class which uses HttpClient
to post data from a Stream
to a remote server.
It has a method PostStreamData(string URL, Stream s)
.
My current code is something like this:
var x = MyLargeObject();
using (var memStream = new MemoryStream())
using (var streamWriter = new StreamWriter(memStream))
{
x.WriteToTextWriter(streamWriter);
customClient.PostStreamData(url, memStream);
}
Internally, PostStreamData
creates a StreamContent()
using the stream object it gets passed in, sets this content as the Content
property of the HttpRequestMessage
object and then finally sends this using SenAsync
method.
Since this uses MemoryStream
, it fails when the object size gets larger than 2GB. See this: Failed to write large amount of data to stream
To overcome this, I used the HugeMemoryStream
class implemented there. But now the issue is that I am using twice the memory. 15GB for the MyLargeObjet
which is already in memory and then another 15GB for the HugeMemoryStream
object created using it.
I think a better solution would be to implement a class based on Stream which uses a buffer of limited size but still allows for objects larger than 2GB. How to implement this? I am looking for some sample code. It doesn't have to be complete, but right now I don't even know how to start.