none
Working Set Memory keeps increasing everytime when receiving data from networkstream RRS feed

  • Question

  • Hi ,

    I have the following code that connects to a server and expects data from it.
    Is it normal that working memory of my program increases every time I receive something from the server I connect to?
    The larger I make the network buffer, the more increase that is added to my working memory (in task manager)
    The data I receive is about 50 bytes. So I minimized that size of the buffer to 128.
    I have tried to clean up every variable I use during the program, but it does not help.
    It is like it is remembering all the bufferdata it received since the program started.
    Can I somehow prevent this from happening.

    I have a basic form with a multiline textbox (Textbox1) on it that is docked to all sides of the form.
    Here is my code.
    Can someone please take a look at it and tell me why the working memory increases?

    Thx


    Imports System.Threading
    
    Imports System.Net
    
    Imports System.Net.Sockets
    
    Imports System.Text
    
    
    
    Public Class Form1
    
    
    
        Private Delegate Sub deltoontekst(ByVal t As String)
    
        Private myclient As TcpClient
    
        Private ns As NetworkStream
    
        Private ReceivedBytes() As Byte
    
        Private ListenThread As Thread
    
        Private maxentriesshown As Integer = 30
    
        Private entries As New Queue()
    
        Private _remote_server_ip As String = "1.2.3.4"
    
        Private t As New Thread(AddressOf Connect)
    
    
    
        Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
    
            t.Abort()
    
        End Sub
    
    
    
        Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
    
            t.Start()
    
        End Sub
    
    
    
    
    
        Private Sub toontekst(ByVal t As String)
    
            Dim tekst As String = ""
    
            Try
    
                If entries.Count < maxentriesshown Then
    
                    entries.Enqueue(t)
    
                Else
    
                    entries.Dequeue()
    
                    entries.Enqueue(t)
    
                End If
    
                For j As Integer = 0 To entries.Count - 1
    
                    tekst &= entries(j).ToString & vbCrLf
    
                Next
    
                TextBox1.Text = tekst
    
            Catch ex As Exception
    
            Finally
    
                tekst = Nothing
    
            End Try
    
        End Sub
    
    
    
    
    
        Private Sub echo(ByVal tekst As String)
    
            Dim dtoontekst As New deltoontekst(AddressOf toontekst)
    
            Dim objarr(0) As Object
    
            objarr(0) = tekst
    
            Try
    
                Me.Invoke(dtoontekst, objarr)
    
            Catch ex As Exception
    
            End Try
    
    
    
            'cleanup
    
            dtoontekst = Nothing
    
            objarr(0) = ""
    
            objarr = Nothing
    
        End Sub
    
    
    
        Public Sub Connect()
    
            Try
    
                echo("Startup")
    
    
    
                'inistialise
    
                ns = Nothing
    
                myclient = Nothing
    
                myclient = New TcpClient()
    
    
    
                'connecteer
    
                echo("Connecting...")
    
                Do While Not myclient.Connected
    
                    Try
    
                        myclient.Connect(_remote_server_ip, 3002)
    
                    Catch ex As Exception
    
                    End Try
    
                    If Not myclient.Connected Then
    
                        echo("No connection!!")
    
                        Thread.Sleep(5000)
    
                    End If
    
                Loop
    
    
    
                echo("connection ok")
    
                'netwerk stream
    
                ns = myclient.GetStream
    
                ns.ReadTimeout = 1000
    
                ns.WriteTimeout = 1000
    
    
    
                echo("start listen")
    
    
    
                Dim returnedtext As String = ""
    
                Dim STXpos As Integer = 0
    
                Dim ETXpos As Integer = 0
    
                ReceivedBytes = Nothing
    
                ReceivedBytes = New Byte(128) {} '4096
    
    
    
                Do While True
    
                    If myclient.Connected And ns.DataAvailable Then
    
                        Try
    
                            'read data
    
                            ReceivedBytes.Initialize()
    
                            ns.Read(ReceivedBytes, 0, ReceivedBytes.Length)
    
    
    
                            'transform data
    
                            returnedtext = Encoding.ASCII.GetString(ReceivedBytes)
    
                            Try
    
                                STXpos = returnedtext.IndexOf(Chr(2))
    
                                ETXpos = returnedtext.IndexOf(Chr(3))
    
                                returnedtext = returnedtext.Substring(STXpos + 1, ETXpos - STXpos - 1)
    
                            Catch ex As Exception
    
                            Finally
    
                                'clean up
    
                                STXpos = Nothing
    
                                ETXpos = Nothing
    
                            End Try
    
                            returnedtext = returnedtext.Replace(vbNullChar, "").Replace(Chr(2), "").Replace(Chr(3), "").Replace(vbCrLf, "").Replace(vbCr, "").Replace(vbLf, "").Trim
    
    
    
                            'show on screen
    
                            echo("received:" & vbCrLf & "START" & vbCrLf & returnedtext & vbCrLf & "STOP")
    
    
    
                            'clean up
    
                            ReceivedBytes.Initialize()
    
                            returnedtext = ""                 
    
                        Catch ex As Exception
    
                            echo("Something went wrong during read!")
    
                            echo(ex.Message)
    
                        End Try
    
                    Else
    
                        Thread.Sleep(1000)
    
                    End If
    
                Loop
    
    
    
            Catch ex As Exception
    
                echo("general error in connect")
    
                echo(ex.Message)
    
            End Try
    
        End Sub
    
    
    
    End Class
    
    


    Tuesday, January 12, 2010 11:51 AM

Answers

  • 1) Try to create smaller repro (avoid WinForms - use console app, avoid error handling, etc.). Also add code which sends the data from server.
    2) Did you use profiler to see where the allocated memory is rooted?

    -Karel
    • Marked as answer by eryang Tuesday, February 9, 2010 5:13 AM
    Monday, January 25, 2010 7:53 PM
    Moderator

All replies

  • 1) Try to create smaller repro (avoid WinForms - use console app, avoid error handling, etc.). Also add code which sends the data from server.
    2) Did you use profiler to see where the allocated memory is rooted?

    -Karel
    • Marked as answer by eryang Tuesday, February 9, 2010 5:13 AM
    Monday, January 25, 2010 7:53 PM
    Moderator

  •  I'm still waiting for the answer.
    Thursday, January 13, 2011 4:17 AM