我正在写一个可怜的负载测试员,我以为我正在管理我的资源(线程池),但是当我运行下面的代码时,我得到一个OutOfMemoryException调用WebClient.DownloadStringAsynch。System.OutOfMemoryException'被抛出 - WebClient.DownloadStringAsynch()
使用.net4.0,但可以移动到4.5。
问:
- 什么是修复?
- 我怎样才能使用HttpWebRequest并发送异步作为webclient的替代品?
关于使用什么.NET 4.5使用的await(着如何.NET4管理线程与非同步调用有什么区别?
static void Main(string[] args) { System.Net.ServicePointManager.DefaultConnectionLimit = 200; while (true) { for (int i = 0; i < 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..."); }
你可以写一个'丰富mans'负载测试仪... LOL – MethodMan 2013-03-01 17:33:03
@DJKRAZE:的确如此! =) – JaJ 2013-03-04 18:32:00