3

我正在尝试编写一个服务器应用程序,它正在侦听特定端口并等待设备访问该端口。设备每 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();
    }
}`

初始服务器运行

初始内存消耗

设备连接 初始连接后的内存消耗

设备连接

多次连接后的内存消耗

4

1 回答 1

1

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

这与内存泄漏相去甚远。您可能正在寻找一个不存在的问题。

在最后一个镜头中,您仍然有一个 10MB 的工作集,这实际上是零。
当您真的想查找/解决内存问题时,请使用分析器。

于 2012-09-24T16:36:30.200 回答