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


i'm writing poor mans load tester , thought managing resources correctly (thread pool) when run following code outofmemoryexception on call webclient.downloadstringasynch.

using .net4.0 move 4.5.

asks:

  • what fix?
  • how use httpwebrequest , send asynch alternative webclient?
  • what using .net 4.5 using await (any difference how .net4 manages threads asynch calls?

    static void main(string[] args) {     system.net.servicepointmanager.defaultconnectionlimit = 200;     while (true)     {         (int = 0; < 100; i++)         {             task.factory.startnew(loadtestasynchnet40);         }         console.writeline(".........................sleeping...............................");         thread.sleep(2);     } }  static void loadtestasynchnet40() {   string url = "http://mysrv.com/api/dev/getthis?stuff=thestuff" + "&_=" + datetime.now.ticks;  // <--- somtimes throws here...     using (var client = new webclient())     {         datetime dt1 = datetime.now;         client.headers["accept"] = "text/xml";         client.downloadstringcompleted += downloadstringcompleted;         console.writeline(datetime.now.tostring("ss:fff") + ", sent ad request...");         client.downloadstringasync(new uri(url), dt1); //<---throws here...     } }  static void downloadstringcompleted(object sender, downloadstringcompletedeventargs e) {     console.writeline("received reponse...");  } 

downloadstringasync create single giant string containing entire response.
if call lots of big responses, run out of memory.

instead, should use httpwebrequest directly.
getresponse() (or begingetresponse()) method gives stream allows read response directly server without buffering in memory.

if still want asyncrony, should move .net 4.5, adds easier-to-use getresponseasync() method (as opposed old apm-based begingetresponse())


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -