Click to See Complete Forum and Search --> : Sockets + Windows Server 2008 = Memory Leak


Torrs
January 29th, 2010, 06:56 AM
Hello,

We have a small service shows continuous nonpaged pool bytes increase from the moment it is started. This is only on a 2008 server (all the others work fine).

The service basically connects a socket to a client to see if it's successful and then closes (most code omitted):


Try
client = New System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp)
client.LingerState = New System.Net.Sockets.LingerOption(False, 0)
client.ReceiveBufferSize = 1024
client.SendBufferSize = 1024
client.Connect(address, _port)
Catch

Finally
'Release the socket.
If client IsNot Nothing AndAlso client.Connected Then
client.Shutdown(System.Net.Sockets.SocketShutdown.Both)
client.Disconnect(True)
client.Close(5)
client = Nothing
End If


It does also call an asynchronous BeginReceive() but I've commented that part out and the leak is still evident. The code is inside a timer, and gets called by a configurable amount. I've tried many things in code, and also upgrading the project to use the 3.5 Framework combined with the suggestions here:

http://msdn.microsoft.com/en-us/magazine/cc163356.aspx

But it still increases by 15.36 bytes every iteration. Some resources just aren't being released?!

Many thanks in advance,

Phil

dglienna
January 29th, 2010, 06:11 PM
Because you are creating a NEW instance each time?

Torrs
February 1st, 2010, 02:56 AM
Because you are creating a NEW instance each time?

Yes, but that instance is closed each time... My apologies, the example may be unclear:


_timer = New System.Threading.Timer(AddressOf Timer, Nothing, 0, Convert.ToInt32(My.Settings.CheckInterval.TotalMilliseconds))

Private Sub Timer()

Connect()

End Sub

Private Sub Connect()

'loop for the number of clients

'Create new instance of socket, connect and dispose.

'/loop

End Sub


Again, this only poses a problem with the 2008 windows server, so the code is sound. Maintaining one open connection for the liftime of the service is not an option (although that would solve it, however starting and stopping the service would probably eat up resources again).

Edit:
I've read up on using a buffer manager as they might be getting pinned in memory... However, I did remove the call to BeginReceive (which should remove the use of buffers) and I still saw an increase.

Torrs
February 24th, 2010, 07:29 AM
Just an update on this for you (and any others reading this) because I hate it when the resolution isn't posted.

I found that setting the socket Disconnect(True) to Disconnect(False) fixed the problem, after reading the following:

"A memory leak occurs when a Winsock API function is called together with the TF_REUSE_SOCKET flag in Windows Server 2008 and in Windows Vista"

http://support.microsoft.com/kb/977332

I'm going to have to assume the hotfix would work, but in my case I'm closing the socket and don't need to leave it availiable for re-use.

Regards,

P