C#套接字应用程序中的内存泄漏

时间:2012-09-24 16:26:45

标签: c# multithreading sockets memory memory-leaks

我正在尝试编写正在侦听特定端口并等待设备命中该端口的服务器应用程序。设备每30秒连接一旦设备连接,设备就会发送其MAC地址。但问题是内存不断增加,永远不会释放。

class Server
{
    Object threadLock = new Object();
    bool stopListening = false;
    Socket clientSocket = null;

    private  void StartDeviceListener()
    {

        try
        {
            // create the socket
            clientSocket = new Socket(AddressFamily.InterNetwork,
                                             SocketType.Stream,
                                             ProtocolType.Tcp);

            // bind the listening socket to the port
            IPEndPoint ep1 = new IPEndPoint(IPAddress.Any, 60000);
            clientSocket.LingerState = new LingerOption(false, 0);

            clientSocket.Bind(ep1);
            clientSocket.Listen(10); //Waiting for Devices to connect.

            do
            {
                // start listening
                Console.WriteLine("Waiting for device connection on {0}....", 60000);
                Socket deviceSocket = clientSocket.Accept();
                //Console.WriteLine(deviceSocket.
                #region ThreadPool

                // ThreadPool.QueueUserWorkItem(ProcessRequest, (Object)deviceSocket);
                Thread ts = new Thread(ProcessRequest);
                ts.IsBackground = true;
                ts.Start((Object)deviceSocket);
                ts.Join();
                #endregion
            } while (!stopListening);

        }
        catch (Exception ex)
        {
            Console.WriteLine("exception... : " + ex.Message);
            StartDeviceListener();
        }

        finally
        {
            if (clientSocket != null) { clientSocket.Close(); clientSocket = null; }
        }

    }

    public void Stop()
    {
        try
        {
            stopListening = true;
            if (clientSocket != null)
            {
                clientSocket.Disconnect(false);
                clientSocket = null;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("exception : " + ex.Message);
        }
    }


    void ProcessRequest(Object args)
    {
        using (Socket deviceSocket = args as Socket)
        {
            try
            {

                //lock the thread while we are creating the client IO Interface Manager
                lock (threadLock)
                {
                    byte[] readBuffer = new byte[1024];
                    // Read from buffer
                    int count = deviceSocket.Receive(readBuffer, 0, readBuffer.Length, SocketFlags.None);
                    String macAddress = "";//mac address sent by the device:
                    if (count > 0)
                    {
                        Encoding encoder = Encoding.ASCII;
                        int size = 0;
                        while (count > 0)
                        {
                            size += count;
                            // get string
                            macAddress += encoder.GetString(readBuffer, 0, count).Trim();
                            // Read from buffer
                            count = 0;
                        }
                        Console.WriteLine(string.Format("{0} trying to connect....", macAddress));
                    }
                    deviceSocket.Close();
                    readBuffer = null;
                }
                //threadLock = null;

            }
            catch (Exception ex)
            {
                Console.WriteLine("exception : " + ex.Message);
            }
        }
        args = null;
    }

  public  void Start()
    {
        StartDeviceListener();
    }
}`

Initial Server running

Initial Memory Consumption

Device Connecting Memory Consumption After Initial Connection

Device Connection

Memory Consumption After Multiple Connection

1 个答案:

答案 0 :(得分:1)

  

但问题是内存不断增加,永远不会释放。

这与内存泄漏相去甚远。你可能正在追逐一个不存在的问题。

在最后一个镜头中,你仍然有一个10MB的工作集,几乎为零 当您确实想要查找/解决内存问题时,请使用分析器。