Advertisement

06.05.2008 at 05:56PM PDT, ID: 23462397
[x]
Attachment Details

Memory leak sending string with .NET TcpClient

Asked by py1rdj in .NET, Microsoft Visual Basic.Net, Microsoft Visual C#.Net

Tags: VB.NET, C#.NET

Hi all.

I have a Windows Service that has to poll several devices every 5 seconds. The devices run an embedded version of Linux running an application that responds to the poll through a TCP/IP connection.
I use threads to be able to poll all the devices without hanging the service, that does several other things ath the same time.
The problem is: I have a memory leak when some of devices are off-line. The TcpClient connection will fail and the memory used by the service starts to increase until it raises an OutOfMemory exception and the service stops working when it reaches about 40MB of used memory.

That's how I call the threads to poll the devices:

Try
            Dim i As Integer
            i = dsContr.Tables.Count
            Dim oDwnlMsg(i) As clControllerPoll
            Dim tPollCont(i) As Thread
            For i = 0 To i - 1
                If _TCPServerHw Is Nothing Then
                    Exit Sub
                End If
                Dim _IPAddress As String = CStr(dsContr.Tables(i).Rows(0).Item("IPAddress"))
                oDwnlMsg(i) = New clControllerPoll
                oDwnlMsg(i).IPAdr = _IPAddress
                tPollCont(i) = New Thread(AddressOf oDwnlMsg(i).SendPollingMsg)
                tPollCont(i).IsBackground = True
                tPollCont(i).Start()
            Next
        Catch ex As Exception
            WriteToLogFile(ex, GetCurrentMethod.Name)
        End Try

And this is the method that tries to send the polling message. If it fails, it returns Nothing. If not, it returns a string sent by the device.

Public Function SendStringMessage(ByVal Message As String, Optional ByVal ControlTimeout As Boolean = False) As String
        Try
            _TCPClient = New TcpClient(_RemoteHost, _RemotePort)
            _NetworkStream = _TCPClient.GetStream()
            If ControlTimeout Then
                _NetworkStream.WriteTimeout = 2000
                _NetworkStream.ReadTimeout = 2000
            End If
            Dim strResponse As String
            'Send a string (newline terminated) to the server.
            Dim writer As New System.IO.StreamWriter(_NetworkStream)
            Dim reader As New System.IO.StreamReader(_NetworkStream)
            writer.Write(Message)
            writer.Flush()
            'Read server response (up to a newline).
            Try
                strResponse = reader.ReadLine
            Catch ex As Exception
                Console.WriteLine("TCP READ ERROR: " & ex.Message)
                strResponse = Nothing
            Finally
                'Close
                writer.Close()
                reader.Close()
                _NetworkStream.Close()
            End Try
            Return strResponse
        Catch ex As Exception
            Console.WriteLine("TCP SendStringMessage ERROR: " & ex.Message)
            Return Nothing
        Finally
            If Not _NetworkStream Is Nothing Then
                _NetworkStream.Close()
                _NetworkStream.Dispose()
                _NetworkStream = Nothing
            End If
            If Not _TCPClient Is Nothing Then
                _TCPClient.Close()
                _TCPClient = Nothing
            End If
        End Try
    End Function

I believe it has something to do with the TCP timeout that takes longer that the next polling message (every 5s).

Any help is appreciated.Start Free Trial
 
Loading Advertisement...
 
[+][-]06.05.2008 at 06:53PM PDT, ID: 21725632

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.06.2008 at 06:12AM PDT, ID: 21728676

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.06.2008 at 07:51AM PDT, ID: 21729611

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.06.2008 at 09:39AM PDT, ID: 21730614

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: .NET, Microsoft Visual Basic.Net, Microsoft Visual C#.Net
Tags: VB.NET, C#.NET
Sign Up Now!
Solution Provided By: py1rdj
Participating Experts: 2
Solution Grade: A
 
 
[+][-]06.06.2008 at 10:00AM PDT, ID: 21730800

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.07.2008 at 10:56AM PDT, ID: 21736308

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.07.2008 at 11:21AM PDT, ID: 21736389

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.08.2008 at 06:01AM PDT, ID: 21738261

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.08.2008 at 06:10PM PDT, ID: 21740221

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.09.2008 at 07:41AM PDT, ID: 21743527

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.09.2008 at 05:54PM PDT, ID: 21747749

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628