gpt4 book ai didi

c# - 内存不足线程 - 性能测试工具

转载 作者:行者123 更新时间:2023-12-03 13:20:44 25 4
gpt4 key购买 nike

我正在创建一个工具来加载测试(发送 http: GETs)并且它运行良好但最终由于内存不足错误而死亡。

询问:如何重置线程以便此循环可以持续运行而不会出错?

    static void Main(string[] args)
{
System.Net.ServicePointManager.DefaultConnectionLimit = 200;

while (true)
{
for (int i = 0; i < 1000; i++)
{
new Thread(LoadTest).Start(); //<-- EXCEPTION!.eventually errs out of memory
}
Thread.Sleep(2);
}
}

static void LoadTest()
{
string url = "http://myserv.com/api/dev/getstuff?whatstuff=thisstuff";

// Sends http get from above url ... and displays the repose in the console....
}

最佳答案

您正在左右和中间实例化 Threads。这很可能是你的问题。你想更换

new Thread(LoadTest).Start();


Task.Run(LoadTest);

这将在 ThreadPool 中的一个线程上运行您的 LoadTest,而不是每次都使用资源来创建一个新线程。然而。这将暴露一个不同的问题。

ThreadPool 上的线程是有限的资源,您希望尽快将 Threads 返回到 ThreadPool。我假设您使用的是同步下载方法而不是 APM 方法。这意味着当请求被发送到服务器时,产生请求的线程正在休眠,而不是去执行其他一些工作。

要么使用(假设 .net 4.5)
var client = new WebClient();
var response = await client.DownloadStringTaskAsync(url);
Console.WriteLine(response);

或者使用回调(如果不是 .net 4.5)
var client = new WebClient();
client.OnDownloadStringCompleted(x => Console.WriteLine(x));
client.BeginDownloadString(url);

关于c# - 内存不足线程 - 性能测试工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15075469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com