.net 中异步SOCKET发送数据时碰到的内存问题

1 篇文章 0 订阅
0 篇文章 0 订阅

做CS的开发一直都是这样的方式:

server端用 C++编写,采用IOCP机制处理大量客户端连接、数据接收发送的问题

client端用 C++ 或C# 写,没什么特殊要求。


最近工作时间上比较宽裕,决定采用新的方式来处理服务端的工作: C# + SOCKET异步机制(.net里没有IOCP的直接支持)

目前正可行性分析阶段,第一步的工作:接收3W个SOCKET连接, 结果还是不错的,很快就建立起来了,速度也可以。

但是第二步测试,接收、发送数据时,就发生了点问题:

   运行的SERVER程序在较短的时间内就占用了大量的内存!


我的测试环境:i3 +2G内存 + Win732位

客户端创建5000个连接,每间隔1秒种对所有的连接发送、接收一次数据。每次发送20bytes到server。

服务端与客户端不在同一台机器上

一般情况下,程序的启动内存占用为4.5M ,运行5分钟后,SERVER程序内存占用超过 100M,并且还在不停的快速增长


在一台服务器上测试(2W个连接),4个小时内,把8G内存全部用光(从任务管理器上看,使用了7.9G内存)



先看SERVER端的完整代码:(大家可以COPY到自己的IDE里直接编译)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace TestAsyncSendMem
{
    class Program
    {
        static TcpListener m_lisnter;
        static AsyncCallback m_acb = new AsyncCallback(DoAcceptSocketCallback);
        static void Main(string[] args)
        {
            m_lisnter = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 8001);
            m_lisnter.Start(5 * 1000);

            try
            {
                m_lisnter.BeginAcceptSocket(m_acb, null);
            }
            catch (Exception ex)
            {
                m_lisnter.Stop();
                m_lisnter = null;

                System.Diagnostics.Debug.WriteLine("BeginAcceptSocket err.Start fail!" + ex);
                return;
            }
            Console.WriteLine("Begin receiving connection... Press any key to quit.");

            Console.ReadKey();

            m_lisnter.Stop();
        }

        static void DoAcceptSocketCallback(IAsyncResult ar)
        {
            System.Net.Sockets.Socket s = null;
            try
            {
                s = m_lisnter.EndAcceptSocket(ar);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("End Accept socket err" + ex);
                s = null;
            }

            try
            {
                m_lisnter.BeginAcceptSocket(m_acb, null);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("after accept client socket,Re beginAcceptSocket fail." + ex);
            }

            if (s != null)
            {
                #region...
                CTcpClientSync c = new CTcpClientSync(s);
                Console.WriteLine(string.Format("accept client.{0}", c.Socket.RemoteEndPoint));

                if (c.BeginRcv() == true)
                {
                    c.OnDisconnected += (CTcpClientSync client) =>
                    {
                        System.Diagnostics.Debug.WriteLine(string.Format("client {0} disconected", client.RemoteIP));
                    };
                }
                else
                {
                    c.Stop();
                    System.Diagnostics.Debug.WriteLine(string.Format("accepted client {0} removed.cannot begin rcv", c.RemoteIP));
                }
                #endregion
            }

        }
    }

    public class CTcpClientSync
    {
        #region delegate

        public delegate void dlgtDisconnected(CTcpClientSync c);
        public event dlgtDisconnected OnDisconnected;

        #endregion

        #region prop

        Socket m_skt = null;
        public Socket Socket { get { return m_skt; } }

        string m_strRemoteIP;
        public string RemoteIP { get { return m_strRemoteIP; } }

        byte[] m_arybytBuf = new byte[1024];

        AsyncCallback m_acb = null;

        #endregion

        public CTcpClientSync(Socket skt)
        {
            m_acb = new AsyncCallback(DoBeginRcvData);

            m_skt = skt;
            try
            {
                m_strRemoteIP = skt.RemoteEndPoint.ToString();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("get remote end point exception."+ ex);
            }

        }

        public void Stop()
        {
            m_skt.Close();
        }

        #region Raise event
        void RaiseDisconnectedEvent()
        {
            dlgtDisconnected handler = OnDisconnected;
            if (handler != null)
            {
                try
                {
                    handler(this);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Raise disconn event exception." + ex.Message);
                }
            }
        }
        #endregion

        public bool BeginRcv()
        {
            try
            {
                m_skt.BeginReceive(m_arybytBuf, 0, m_arybytBuf.Length, SocketFlags.None, m_acb, null);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("BeginRcv exception." + ex);
                return false;
            }

            return true;
        }

        void DoBeginRcvData(IAsyncResult ar)
        {
            int iReaded = 0;
            try
            {
                iReaded = m_skt.EndReceive(ar);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("BeginRcv exception." + ex);

                Stop();
                RaiseDisconnectedEvent();

                return;
            }

            if (iReaded > 0)
            {
                //收到后发送回一个数据包
                SendAsync(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 });

                if (BeginRcv() == false)
                {
                    Stop();
                    RaiseDisconnectedEvent();
                }
            }
            else
            {
                Stop();
                RaiseDisconnectedEvent();
            }
        }

        public bool SendAsync(byte[] bytsCmd)
        {
            SocketAsyncEventArgs e = new SocketAsyncEventArgs();
            try
            {
                e.SetBuffer(bytsCmd, 0, bytsCmd.Length);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("SetBuffer exception." + ex);
                return false;
            }            

            try
            {
                if (m_skt.SendAsync(e))
                {//Returns true if the I/O operation is pending.
                    return true;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("SendAsync exception." + ex);                

                return false;
            }

            //Returns false if the I/O operation completed synchronously. 
            //In this case, The SocketAsyncEventArgs.Completed event on the e parameter will not be raised and 
            //the e object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation.

            return true;

        }
    }
}

 
 

.net中的内存是由系统自行回收的,一旦一个对象(内存块)发现没有被其它任何人使用(引用)则会被回收。

当满足以下条件之一时将发生垃圾回收:

  • The system has low physical memory.

  • The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This means that a threshold of acceptable memory usage has been exceeded on the managed heap.This threshold is continuously adjusted as the process runs.

  • The GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.

具体请看这里(垃圾回收

条件1:当物理内存极低时会调用

如上所说,我在一个服务器上测试此程序,8G内存,2W个连接,每5秒种给所有的连接发送一次。在大概4个小时就把所有的内存完了。从任务管理器上看,内存占用了7.9个G。并且,此时SERVER程序已经无法接受发送来自客户端的数据了。所以,按这个情况,内存回收肯定应该工作了!但没有!

条件2:已经在托管heap上分配的对象所占用的内存超过一个阀值时会调用。这个阀值会动态变更。

如上一个测试,物理内存都已经用光了,并导致程序不能正常运行了。这个阀值还没有超过?!!这个阀值是怎么定的呢?(需要找一下文档,网友了解的提供一下 :))

假定是因为某种原因,GC没有执行。那我们手动的执行一下,添加一个全局变量 s_iRcvTimes  ,每接收5000次就执行一下回收

        public bool SendAsync(byte[] bytsCmd)
        {
            if (s_iRcvTimes > 5000)
            {
                s_iRcvTimes = 0;
                GC.Collect(2);
            }
            s_iRcvTimes += 1;
		
	  ...//原来的代码省略
 	}

测试结果如下:(程序启动后,每过一段时间记录一下SERVER程序的内存占用情况)

程序的启动内存占用为:4.5M 

序号时间时间间隔内存占用内存增长
116:07:001分钟22,023K--
216:08:001分钟22,900K 677K
316:10:002分钟26,132K 3,232K
416:12:002分钟30,172K 4,040K
516:17:005分钟116,032K 85,860K
616:22:005分钟200,146K 84,114K
716:27:005分钟274,120K 73,974K

内存占用:对应时刻Server程序所占用的内存(从windows任务管理器看到的数据)


从测试结果来看,应该没有起到作用!


我感觉,还是程序有问题!理论上来说,一旦内存不够,则系统自动进行回收,但是,为什么这里的情况不进行回收呢?!!MSDN里有这样一句话:

When a garbage collection is triggered, the garbage collector reclaims the memory that is occupied by dead objects.

所以,有可能有些对象根本都没有成为 dead objects,从而使GC没办法将其回收。


OK ,那先找到内存爆涨的地方,再来分析为什么这些对象没办法成为 dead object !


内存出问题,那肯定是NEW出来的没有被回收!

程序中,NEW的地方有几个:

1.收到数据后,回送的地方:SendAsync(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 });

2.接收数据里的异步操作支持:SocketAsyncEventArgs e = new SocketAsyncEventArgs();


下意识的觉得第二个地方比较可疑。所以,修改此处并测试。 内存果然有明显的提升:

两个测试:

1. 5K个连接,每次给每个连接发送20个bytes,发完后停止1秒再继续。

2. 1K个连接,每次给每个连接发送20个bytes,发完后停止1秒再继续。

测试的结果,可以坚持10分钟以上没有任何问题,这期间内存一直在30M以下,启动后当所有的连接连上来之后(开始发送数据之前)程序大概占用20M


其实MSDN里也有对这个的例子,例子中对这个 SocketAsyncEventArgs也是给缓存起来,而不是直接每次都NEW一个对象。它的例子详细请看这里


修改后的代码如下:添加一个变量

public static List<SocketAsyncEventArgs> s_lst = new List<SocketAsyncEventArgs>();

然后修改SendAsync 函数如下:

  
public bool SendAsync(byte[] bytsCmd)
        {
              SocketAsyncEventArgs e = null;//new SocketAsyncEventArgs();
            lock (Program.s_lst)
            {
                if (Program.s_lst.Count > 0)
                {
                    e = Program.s_lst[Program.s_lst.Count - 1];
                    Program.s_lst.RemoveAt(Program.s_lst.Count - 1);
                }
            }
            if (e == null)
            {
                e = new SocketAsyncEventArgs();
                e.Completed += (object sender, SocketAsyncEventArgs _e) =>
                {
                    lock (Program.s_lst)
                        Program.s_lst.Add(e);
                };
            } 

            try
            {
                e.SetBuffer(bytsCmd, 0, bytsCmd.Length);
            }
            catch (Exception ex)
            {
                lock (Program.s_lst)
                    Program.s_lst.Add(e);
                System.Diagnostics.Debug.WriteLine("SetBuffer exception." + ex);
                return false;
            }

            try
            {
                if (m_skt.SendAsync(e))
                {//Returns true if the I/O operation is pending.
                    return true;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("SendAsync exception." + ex);                

                return false;
            }

            //Returns false if the I/O operation completed synchronously. 
            //In this case, The SocketAsyncEventArgs.Completed event on the e parameter will not be raised and 
            //the e object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation.
            lock (Program.s_lst)
                Program.s_lst.Add(e);
            return true;

        } 

方法应该比较简单:不是每次都创建新的对象,而是用完后保存起来给下次调用时使用。


现在的问题比较明确了:

为什么这里的 new SocketAsyncEventArgs() 会无法被回收呢? 也就是说:一直被某个对象引用着,无法成为 dead object.


明天继续... :)


程序中的代码也可以这里 下载 


有问题请联系我,谢谢

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值