Memory Leak in BeginSend?

G

Guest

Hi !
It appears that there is some kind of memory leak when using
Socket.BeginSend and AsyncCallback functionality (probably even in
Socket.BeginReceive)
Below is the sample code that illustrates the problem.
If you run it under CLR Profiler for 5-10 minutes, then look at "Objects by
address", you will see that there is a lot of OverlappedAsyncResult objects
(and related) in generation #2 area of the Heap. Why would that be this way?
If you run the sample for few days, and look at ".NET CLR Memory/ Gen 2 Heap
Size" for the application, you will see that the size of that section of the
heap is enourmous.

Am I applying AsyncCallback pattern incorrectly, or there is some problem in
CLR?

Thank you

Alexander Safronov

----------------------------------------------
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace SocketBug
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class App
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Socket s = new
Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
s.Bind(new IPEndPoint(IPAddress.Loopback,666));
s.Connect(new IPEndPoint(IPAddress.Loopback,666));
Random r = new Random();
while (true)
{
byte[] bufferToSend = new byte[r.Next(100,600)]; // this is
allocated each time to make Garbage Collection more or less active
r.NextBytes(bufferToSend);
AsyncCallback callback = new AsyncCallback(onSent);
s.BeginSend(bufferToSend,0,bufferToSend.Length,0,callback,s);
Thread.Sleep(10);
}
}
static void onSent(IAsyncResult ar)
{
((Socket)ar.AsyncState).EndSend(ar);
}
}


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top