Mininova Web
Mininova Web

Reputation: 75

System.OutOfMemoryException' was thown after 10PM. I am not able to catch actual error

On server following software installed:

  1. Windows Server 2012, SQL Server
  2. Symentec Antivirus
  3. Finger print reader based on Nginx, MS Sql Server DB

I am not able to find the error. Threads throws error System.Outofmemory and Server have 6GB free memory. Max data received by one socket is less than 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");
        }
    } 
}

Upvotes: 2

Views: 304

Answers (1)

VMAtm
VMAtm

Reputation: 28355

Your code is throwing the OutOfMemoryException because of many-many-many threads being created in it. Look:

// 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
}

This is the worst thing you can do in your code. Your application starves because of tons of threads being concurrently trying to listen the TcpClients. Recommended number of threads in your application is equal to the number of cores on your server machine (Managed Threading Best Practices).

Your really should use some thread pooling, either yourself written (personally I don't recommend it) or already built in ones (How to: Use a Thread Pool (C# and Visual Basic)).

If you are using .NET 4.5, use Tasks library to simplify your code.
Right now your application is unstable and error-prone.

If for somewhat reason you still think that you should have to create a Thread each time you are calling the TcpClient (which is completely wrong, as I think), you should read this manual:

How to: Create and Terminate Threads (C# Programming Guide)

Upvotes: 2

Related Questions