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

C#套接字应用程序中的内存泄漏,c#,multithreading,sockets,memory,memory-leaks,C#,Multithreading,Sockets,Memory,Memory Leaks,我正在尝试编写一个服务器应用程序,该应用程序正在侦听某个特定端口,并等待设备访问该端口。设备连接后,每30秒连接一次,设备发送其MAC地址。但问题是,内存一直在增加,而且从未释放出来 class Server { Object threadLock = new Object(); bool stopListening = false; Socket clientSocket = null; private void StartDeviceListener()

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

但问题是,内存一直在增加,而且从未释放出来

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();
    }
}`
这与内存泄漏还有很大差距。你可能在追求一个不存在的问题

在最后一张照片中,您仍然有一个10MB的工作集,这实际上是零。

当您确实想要查找/解决内存问题时,请使用探查器

@Dimitry:如果你必须手动开始垃圾收集,那么你正在做一些非常糟糕或非常奇怪的事情。这真的是个例外。亲自调用垃圾收集器是个好主意。尝试使用tousends连接添加100kb,这是内存泄漏吗?@rahul-这看起来不是真正的问题。内存只在需要时收集,或者GC需要时收集。要知道这是否是一个真正的内存泄漏,你需要给我们一个内存分析器,因为它在应用程序的开始和结束时都会挂接到应用程序中,并能告诉你什么时候发生了真正的内存泄漏。在分析时使用GC.Collect()非常好,因为这样做只是为了查看等待收集的内存。在生产应用程序中不会这样做。此外,虚拟内存是什么并不重要,这不是一个实际分配的内存数。您是否尝试过不创建自己的线程并使用线程池(更好一点)甚至任务(更好一点,但取决于您使用的.Net版本)来完成这项工作?只需连接一个设备就可以了;但是如果我有像4K-5K这样的设备试图连接,问题会被放大。放大了多少,它会变平还是会出现OOM异常?如果我让服务器运行一天,它会一直增加到1GB;在那之后,我无法连接任何设备。因此,我有一个计划任务,每天重新启动m/c和服务器一次,但我猜这是另一个版本(更多代码),而不是发布在这里的版本。我看到一个单线程服务器,但没有明显的漏洞。问题可能在其他代码中。。。