CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2010
    Posts
    3

    Sockets + Windows Server 2008 = Memory Leak

    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):

    Code:
    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
    Last edited by Torrs; February 1st, 2010 at 07:12 AM. Reason: Put code in code blocks

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Sockets + Windows Server 2008 = Memory Leak

    Because you are creating a NEW instance each time?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jan 2010
    Posts
    3

    Re: Sockets + Windows Server 2008 = Memory Leak

    Quote Originally Posted by dglienna View Post
    Because you are creating a NEW instance each time?
    Yes, but that instance is closed each time... My apologies, the example may be unclear:

    Code:
    _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.
    Last edited by Torrs; February 1st, 2010 at 07:10 AM. Reason: put code in code blocks

  4. #4
    Join Date
    Jan 2010
    Posts
    3

    Re: Sockets + Windows Server 2008 = Memory Leak

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured