Is this a memory leak?

G

Guest

Hi,

I've been having problem with a socket listener as a windows service consuming all available memory. The following code reproduces my problem:

Dim lSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)
lSocket.Bind(New IPEndPoint(IPAddress.Any, 16005))
lSocket.Listen(5)

While (True)
Dim cSocket As Socket = lSocket.Accept
cSocket = lSocket.Accept
cSocket.Shutdown(SocketShutdown.Both)
cSocket.Close()
CType(cSocket, IDisposable).Dispose()
cSocket = Nothing
End While

Running this code from a button on a form and making 100's of sequencial connections to the listen socket causes the process to consume 4-8k of memory about every 6-8 connections. This memory is never freed.

Im using .net 1.1 and VB on Windows 2k Pro. Is this a memory leak in .net or a GC problem? Should I be looking at making calls to the GC?

Regards, Ben.
 
W

Wiktor Zychla

While (True)
Dim cSocket As Socket = lSocket.Accept
cSocket = lSocket.Accept
cSocket.Shutdown(SocketShutdown.Both)
cSocket.Close()
CType(cSocket, IDisposable).Dispose()
cSocket = Nothing
End While

you are accepting twice so I guess the first accepted socket is never
disposed.
shouldn't it be just

Dim cSocket As Socket
cSocket = lSocket.Accept

?

Wiktor Zychla
 
G

Guest

Wiktor Zychla said:
you are accepting twice so I guess the first accepted socket is never
disposed.
shouldn't it be just

Dim cSocket As Socket
cSocket = lSocket.Accept

?

Wiktor Zychla

Ack, sorry. Please ignore the second accept call. That'll teach me to mess with the code before posting it :(

The problem still occurs however.

Ben.
 
W

Wiktor Zychla

The problem still occurs however.
Ben,

I've written two short programs to test your problem, a server and a
client (sources below). The client connects to the server in an infinite
loop. Unfortunately, I am also stuck. When run on a single machine, the
client throws an exception after about 3950 succesful connections.

Only one usage of each socket address(protocol/network address/port)is
normally permitted
at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)

I guess that some winsock resources are not released. And I guess I also
need a helpful hand on that.

Regards,
Wiktor

a server:

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

class Q
{
public static void Main()
{
Socket lSocket = new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
lSocket.Bind(new IPEndPoint(IPAddress.Any, 16005));
lSocket.Listen(5);

while (true)
{
Socket cSocket;
cSocket = lSocket.Accept();
if ( cSocket.Connected )
{
Console.WriteLine( "Accepted..." );
cSocket.Shutdown(SocketShutdown.Both);
cSocket.Close();
}
}
}
}

and a client:

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

class Q
{
public static void Main()
{
for ( int i=0; i<10000; i++ )
{
IPAddress myIP=IPAddress.Parse("127.0.0.1");
IPEndPoint myServer=new IPEndPoint(myIP,Int32.Parse("16005"));

Socket socket=new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
socket.Connect(myServer);

Console.WriteLine( "Connected..." );

socket.Shutdown(SocketShutdown.Both);
socket.Close();

}
}
}
 
G

Guest

Wiktor Zychla said:
Ben,

I've written two short programs to test your problem, a server and a
client (sources below). The client connects to the server in an infinite
loop. Unfortunately, I am also stuck. When run on a single machine, the
client throws an exception after about 3950 succesful connections.

Only one usage of each socket address(protocol/network address/port)is
normally permitted
at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)

I guess that some winsock resources are not released. And I guess I also
need a helpful hand on that.

Regards,
Wiktor

a server:

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

class Q
{
public static void Main()
{
Socket lSocket = new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
lSocket.Bind(new IPEndPoint(IPAddress.Any, 16005));
lSocket.Listen(5);

while (true)
{
Socket cSocket;
cSocket = lSocket.Accept();
if ( cSocket.Connected )
{
Console.WriteLine( "Accepted..." );
cSocket.Shutdown(SocketShutdown.Both);
cSocket.Close();
}
}
}
}

and a client:

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

class Q
{
public static void Main()
{
for ( int i=0; i<10000; i++ )
{
IPAddress myIP=IPAddress.Parse("127.0.0.1");
IPEndPoint myServer=new IPEndPoint(myIP,Int32.Parse("16005"));

Socket socket=new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
socket.Connect(myServer);

Console.WriteLine( "Connected..." );

socket.Shutdown(SocketShutdown.Both);
socket.Close();

}
}
}

Hi Wiktor,

Thanks for having a look at the problem. This would cetainly explain my memory consumption problems. I'll keep testing.

Regards, Ben.
 
W

Wiktor Zychla

Thanks for having a look at the problem. This would cetainly explain my
memory consumption problems. I'll keep testing.

please write a new message to this newsgroup if you find anything.
 

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