.NET UDP套接字发送增加的内存使用量 [英] .NET UDP Socket Send Increasing Memory Usage

查看:40
本文介绍了.NET UDP套接字发送增加的内存使用量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个UDP客户端&的问题.服务器频繁交换消息,并且两个实体的内存使用量都以每秒约8K的速度增长(尽管最终,这取决于它们之间的通信速率).

I'm seeing an issue where I have a UDP client & server exchanging messages frequently and the memory usage for both entities is increasing at a rate of approximately 8K per second (althoughly ultimately, this depends on the rate of commuinications between them) as observed in the Task Manager.

为了尽可能简单地说明这一点,我基于MSDN使用UDP服务创建了一个示例 http://msdn.microsoft.com/en-us/library/tst0kwb1.aspx .

To illustrate this as simply as possible, I've created a sample based upon the MSDN Using UDP Services http://msdn.microsoft.com/en-us/library/tst0kwb1.aspx.

服务器:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class UDPListener
{
private const int listenPort = 11000;

private static void StartListener()
{
    bool done = false;

    UInt32 count = 0;
    UdpClient listener = new UdpClient(listenPort);
    IPEndPoint groupEP = new IPEndPoint(IPAddress.Loopback, listenPort);

    try
    {
        while (!done)
        {
            byte[] bytes = listener.Receive(ref groupEP);

            if ("last packet" == System.Text.Encoding.UTF8.GetString(bytes))
            {
                done = true;
                Console.WriteLine("Done! - rx packet count: " + Convert.ToString(count));
            }
            else
            {
                count++;
            }
        }

    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
    finally
    {
        listener.Close();
    }
}

public static int Main()
{
    StartListener();

    return 0;
}

}

和客户:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace UDPSender
{
class Program
{
    static void Main(string[] args)
    {
        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
            ProtocolType.Udp);

        IPAddress broadcast = IPAddress.Parse(IPAddress.Loopback.ToString());

        byte[] sendbuf = Encoding.ASCII.GetBytes("test string");
        IPEndPoint ep = new IPEndPoint(broadcast, 11000);

        for (int i = 0; i < 500; i++)
        {
           s.SendTo(sendbuf, ep);

            System.Threading.Thread.Sleep(50);
        }

        s.SendTo(Encoding.ASCII.GetBytes("last packet"), ep);

        s.Dispose();
    }
}
}

我尝试过直接使用Socket接口和UDPClient,每次传输后都删除客户端套接字,显式GC.Collect等都无效.

I've tried both using the Socket interface directly and UDPClient, dropping the client socket after each transmission, explicit GC.Collect etc. to no avail.

任何想法都在这里-我不敢相信这是.NET的一个基本问题,我的代码/示例肯定有问题....

Any ideas what's going on here - I can't belive this is a fundamental issue with .NET, there must be an issue with my code/the sample....

推荐答案

尝试一下:


bytes = null;

这篇关于.NET UDP套接字发送增加的内存使用量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆