System.OutOfMemoryException& # 39;晚上10点后就倒下了.我不能捕捉到实际的错误

本文关键字:不能 错误 OutOfMemoryException 晚上 10点 System | 更新日期: 2023-09-27 18:18:51

服务器上安装了以下软件:

    Windows Server 2012, SQL Server
  1. 赛门铁克防病毒软件
  2. 基于Nginx, MS Sql Server DB的指纹读取器

我找不到错误。线程抛出错误。Outofmemory和Server有6GB的空闲内存。一个套接字接收的最大数据量小于4kb

class ServerModule
{
    TcpListener serverSocket;
    public void StartServer()
    {
        serverSocket.Start();
        while (true)
        {
            try
            {
                if (cmd == -1)
                {
                    break;// stop loop
                }
                Console.WriteLine("Listening");
                TcpClient tc= serverSocket.AcceptTcpClient();
                System.Threading.Thread obj_thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(ProcessData));
                obj_thread.Start(tc);
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError("StartServer::", ex);
            }
        }
    }
    public void ProcessData(object ob)
    {
        TcpClient tcp_socket = (TcpClient)ob;
        NetworkStream ns = null;
        try
        {
            ns = tcp_socket.GetStream();
            int num = 0;
            byte[] obj_data = new byte[tcp_socket.ReceiveBufferSize];
            num = ns.Read(obj_data, 0, tcp_socket.ReceiveBufferSize);
            tcp_socket.Close();
            tcp_socket = null;
        }
        catch (Exception ex)
        {
            ErrorLogger.LogError("ProcessData::", data_from_device, ex);
            if (tcp_socket != null)
            {
                tcp_socket.Close();
                tcp_socket = null;
            }
            Console.WriteLine("Close with exception");
        }
    } 
}

System.OutOfMemoryException& # 39;晚上10点后就倒下了.我不能捕捉到实际的错误

您的代码抛出了OutOfMemoryException,因为其中创建了许多许多线程。看:

// in a infinite loop
while (true)
{
    ...
    // for each TcpClient
    TcpClient tc= serverSocket.AcceptTcpClient();
    // you create a thread!
    System.Threading.Thread obj_thread = new System.Threading.Thread
}

这是您在代码中可以做的最糟糕的事情。您的应用程序会饿死,因为有大量线程同时尝试侦听TcpClients。应用程序中建议的线程数等于服务器机器上的内核数(托管线程最佳实践)。

你真的应该使用一些线程池,无论是你自己写的(我个人不推荐)还是已经内置的(如何:使用线程池(c#和Visual Basic))。

如果你使用。net 4.5,使用Tasks库来简化你的代码。
现在你的应用程序不稳定而且容易出错。

如果出于某种原因,你仍然认为你应该在每次调用TcpClient时创建一个Thread(这是完全错误的,我认为),你应该阅读本手册:

如何创建和终止线程(c#编程指南)
相关文章: