我需要测试客户端服务器应用程序。我有服务器运行在某个端口上,需要创建大约2000个连接服务器的客户端。对于这个我尝试使用下面的代码创建线程时系统内存不足
class Program
{
/// <summary>
/// Sample client code that makes gRPC calls to the server.
/// </summary>
public static void Main(string[] args)
{
for (int i = 0; i <= 2000; i++)
{
CalThread cThread = new CalThread(i);
} // Exception Raised Here
}
}
class CalThread
{
public CalThread(int clientID)
{
Thread thread = new Thread(() => getDataFromServer(clientID));
thread.Start();
}
public void getDataFromServer(int clientID)
{
try
{
//Channel channel = new Channel("192.168.0.123:50051", ChannelCredentials.Insecure);
while (true)
{
//Some code to connect to server and fetch data
Thread.Sleep(15000);
}
}
catch (Exception ex)
{
Thread.Sleep(15000);
Console.WriteLine(ex.Message);
}
}
}
这里例外发生在System.OutOfmemory
for loop of Main method
但是我已经检查应用程序消耗时此异常只筹集到110 MB
内存中创建在C#应用程序2000多个线程?
为什么c#不让我在数字中创建线程..? 我也试过Thread Pool
但不能正常工作...
我可以使用'ThreadPool' –