Cipriyan
Cipriyan

Reputation: 11

Out of memory exception when writing a large file (300MB) to memory stream

I'm getting an Out of Memory Exception when using Http.Post of a large file. I'm getting this exception when I tried to write the filestream data to the memory stream.

Upvotes: 1

Views: 4343

Answers (3)

RoundOutTooSoon
RoundOutTooSoon

Reputation: 9891

None of the above answers work for me, this one does the job:

http://blogs.msdn.com/b/johan/archive/2006/11/15/are-you-getting-outofmemoryexceptions-when-uploading-large-files.aspx

These lines are the key:

    HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(yourUri);
    wr.KeepAlive = false;
    wr.Timeout = System.Threading.Timeout.Infinite;
    wr.ProtocolVersion = HttpVersion.Version10;

And here:

wr.AllowWriteStreamBuffering = false;

Hope this can help someone out there.

Upvotes: 1

feroze
feroze

Reputation: 7594

Set HttpWebRequest.AllowWriteStreamBuffering=false. This will cause HWR to send the data immediately, instead of buffering it in memory.

Upvotes: 0

StriplingWarrior
StriplingWarrior

Reputation: 156469

Obviously putting 300mb of data into memory is causing the CLR to reach its maximum memory footprint. Have you considered writing the file to some other kind of stream to avoid having it all in memory at the same time? Perhaps you could write the file directly to disk, and then process the file in chunks afterward, rather than loading the whole thing into memory at the same time.

Upvotes: 2

Related Questions