genxgeek
genxgeek

Reputation: 13347

System.OutOfMemoryException' was thrown - WebClient.DownloadStringAsynch()

I'm writing a poor mans load tester and I thought I was managing my resources correctly (thread pool) but when I run the following code I get an OutOfMemoryException on my call to WebClient.DownloadStringAsynch.

Using .net4.0 but could move to 4.5.

ASKs:

Upvotes: 1

Views: 2316

Answers (1)

SLaks
SLaks

Reputation: 887215

DownloadStringAsync will create a single giant string containing the entire response.
If you call that for lots of big responses, you will run out of memory.

Instead, you should use HttpWebRequest directly.
Its GetResponse() (or BeginGetResponse()) method gives you a stream that allows you to read the response directly from the server without buffering it in memory.

If you still want asyncrony, you should move the .Net 4.5, which adds the easier-to-use GetResponseAsync() method (as opposed to the old APM-based BeginGetResponse())

Upvotes: 3

Related Questions