yuvraj oak
yuvraj oak

Reputation: 1

How to avoid Memory leak in .NET SSLStream

I have designed an SSL Server in VB.NET.

However, when I receive multiple TCP requests (10s per second) to my server, I get a memory leak. Means memory increases every time I receive a request.

Procedure to simulate error:

  1. Listen to HTTPS (443) port.
  2. Make multiple request from Browser.
  3. Close connection after processing request.

This leads to increased memory usage by the program at every request.

I tried running GC.collect() but nothing works.

Kindly find attached following code:

Private HTTPS_Listener As Socket = Nothing

Private Sub ListenS()
    Try
        HTTPS_Listener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Dim ep As New Net.IPEndPoint(IPAddress.Any, 443)
        HTTPS_Listener.Bind(ep)
        HTTPS_Listener.Listen(1024)
        HTTPS_Listener.BeginAccept(New AsyncCallback(AddressOf DoSecureCallBack), Nothing)
    Catch ex As Exception
        Console.WriteLine(ex.ToString)
    End Try
End Sub

Private Sub DoSecureCallBack(ByVal ar As IAsyncResult)
    Dim sock As Socket = Nothing
    Try
        sock = HTTPS_Listener.EndAccept(ar)
        Console.WriteLine(vbCrLf + vbCrLf + vbCrLf + "Conn received " + Threading.Interlocked.Increment(iCon).ToString)
    Catch ex As Exception
        Add_Log(0, "ERROR", "WebServer", "Could not accept web connection." + vbCrLf + ex.Message)
        Exit Sub
    Finally
        HTTPS_Listener.BeginAccept(New AsyncCallback(AddressOf DoSecureCallBack), Nothing)
    End Try

    Dim myNS As NetworkStream = Nothing
    Dim mySSLStream As SslStream = Nothing

    'Process Web Request
    Dim bytesRead As Integer = -1
    Try
        myNS = New NetworkStream(sock, False)
        mySSLStream = New SslStream(myNS, False)
        mySSLStream.AuthenticateAsServer(cert_Socket, False, SslProtocols.Tls12, False)

        mySSLStream.Write(btOut, 0, btOut.Length)
    Catch ex As Exception
        If bytesRead > 0 Then
            Console.WriteLine("Could not process Web Request." + vbCrLf + ex.Message)
        End If
    Finally
        If Not mySSLStream Is Nothing Then
            mySSLStream.Close()
            mySSLStream.Dispose()
        End If
        If Not myNS Is Nothing Then
            myNS.Close()
            myNS.Dispose()
        End If
        If Not sock Is Nothing Then
            sock.Close()
        End If
        sock = Nothing
    End Try
End Sub 'DoAcceptSocketCallback

Upvotes: -1

Views: 696

Answers (1)

Ben Adams
Ben Adams

Reputation: 3431

Check GC.GetTotalMemory to ensure you have a leak; it may just be allocating more memory for the process based on there being lots available and that being less expensive than reusing the memory. If that is the case then at some point the memory increase will plateau as it switches over to reusing the memory.

Also if you can switch to .NET Core 3.0 and use Async/Await rather than Begin/End for async it should additionally use less memory per accept.

Upvotes: 0

Related Questions