VB.NET OutOfMemoryException Error

I use Socket to communicate with the hardware called MW100 and read the data from it by calling Sub  SendRequestToMW100 every five seconds.

The Initial connection is good, now after about 25 Minutes running time, I got the System.OutOfMemoryException error from the line: Dim inStream(10024) As Byte


Could anybody help me with this?

My Code likes this:

 Dim clientSocket As New System.Net.Sockets.TcpClient()
 Dim serverStream As NetworkStream

    Public Sub SendRequestToMW100()

        Dim term As String = Chr(10)

        Dim serverStream As NetworkStream = clientSocket.GetStream()
        Dim outStream As Byte() = _
        System.Text.Encoding.ASCII.GetBytes("FD0, 001, 060" & term)

           Try              
                serverStream.Write(outStream, 0, outStream.Length)

            Catch ex As Exception
                MsgBox(ex.Message)

            End Try
            serverStream.Flush()

            Dim inStream(10024) As Byte
            Try
                serverStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize))

            Catch ex As Exception
                MsgBox(ex.Message)

            End Try
            Dim returndata As String = _
            System.Text.Encoding.ASCII.GetString(inStream)
            msg("Data from Server : " + returndata)
       
    End Sub
charlieb01Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
You do not call Close or Dispose on you NetworkStream when you are finished with it, so its data stays in memory.

Whenever you see that a class that has a Dispose method (at least in the Framework methods), it is recommended to call it (or Open, which does the same thing form streams) before you lose your variable. That forces the object to cleanup behind itself.

A simple serverStream.Close before the End Sub would probably solve your problem.
0
charlieb01Author Commented:
I tried putting serverStream.Close at the place indicated but then I did not get any returned data at all, unless I got one reading and missed it. Is there a  better place to put the Close. All of my code from the module is here:
Imports System.Net.Sockets
Imports System.Text


Module modMW100Communication

    Dim clientSocket As New System.Net.Sockets.TcpClient()
    Dim serverStream As NetworkStream

    Public RemoteHostAddress As String = My.Settings.MW100IPAddr

    Const portNumber As Integer = 34318

    Public MW100SourceIsMW100%

    Public Sub ConnectToMW100()

        If MW100SourceIsMW100 Then
            Try
                clientSocket.Connect(RemoteHostAddress, portNumber)

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub

    Public Sub SendRequestToMW100()

        Dim term As String = Chr(10)

        Dim serverStream As NetworkStream = clientSocket.GetStream()
        Dim outStream As Byte() = _
        System.Text.Encoding.ASCII.GetBytes("FD0, 001, 060" & term)

        If MW100SourceIsMW100 Then
            Try
                serverStream.Write(outStream, 0, outStream.Length)

            Catch ex As Exception
                MsgBox(ex.Message)

            End Try
            serverStream.Flush()

            Dim inStream(10024) As Byte
            Try
                serverStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize))

            Catch ex As Exception
                MsgBox(ex.Message)

            End Try
            Dim returndata As String = _
            System.Text.Encoding.ASCII.GetString(inStream)
            msg("Data from Server : " + returndata)
        End If
        serverStream.Close()          'TRIED THIS BUT THEN DID NOT GET ANY DATA RETURNED AT ALL OR PERHAPS I GOT ONE READING
    End Sub
    Public Sub CloseMW100Socket()
        If MW100SourceIsMW100 Then
            clientSocket.Close()
        End If
    End Sub
    Public Sub msg(ByVal mesg As String)
        Dim aryMessage() As String
        Dim longterm As String = Chr(13) & Chr(10)
        Dim intIndex As Integer

        If InStr(mesg, ": EA") > 0 Then
            If InStr(mesg, "N 001") > 0 Then
                aryMessage = Split(Trim(mesg), longterm)
                For intIndex = 0 To 64
                    If InStr(aryMessage(intIndex), "EN") > 0 Then
                        Exit Sub
                    Else
                        If InStr(aryMessage(intIndex), "+") > 0 Then
                            'Debug.Print(Mid(aryMessage(intIndex), InStr(aryMessage(intIndex), "+"), 10).ToString)
                            'Form1.TextBox1.Text = Form1.TextBox1.Text + Environment.NewLine + Mid(aryMessage(intIndex), InStr(aryMessage(intIndex), "+"), 10).ToString

                            AinElec(intIndex - 3) = Mid(aryMessage(intIndex), InStr(aryMessage(intIndex), "+"), 10).ToString
                        ElseIf InStr(aryMessage(intIndex), "-") > 0 Then
                            'Debug.Print(Mid(aryMessage(intIndex), InStr(aryMessage(intIndex), "-"), 10).ToString)
                            'Form1.TextBox1.Text = Form1.TextBox1.Text + Environment.NewLine + Mid(aryMessage(intIndex), InStr(aryMessage(intIndex), "-"), 10).ToString
                            AinElec(intIndex - 3) = Mid(aryMessage(intIndex), InStr(aryMessage(intIndex), "-"), 10).ToString
                        End If

                    End If
                Next
            End If

        End If
    End Sub
End Module

Open in new window

0
Jacques Bourgeois (James Burger)PresidentCommented:
The information I gave you was with my experience with hundred of classes in the Framework.

Looking at the documentation, I see that there is an alternate Close method that you can use with the NetworkStream. You can specify a number of milliseconds to wait before really closing, which leaves time for the Stream to finish its job. The time required has to be found through trial and error. You can start with 1 second:

serverStream.Close(1000)
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

charlieb01Author Commented:
Sorry for the delay in response - got sidetracked. Adding the delay didn't help at all
0
CodeCruiserCommented:
Try adding

inStream = Nothing
GC.Collect


as the last two lines in your sub.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ArkCommented:
Why are you declaring serverStream twice - at module level and at sub level?
0
ArkCommented:
PS. Try use dynamic array instead of static:
Dim inStream() As Byte
Dim buffSize=clientSocket.ReceiveBufferSize
If buffSize > 0 Then
    Redim inStream(buffSize-1)
            Try
                serverStream.Read(inStream, 0, buffSize)
'rest of code
End If

Open in new window

0
charlieb01Author Commented:
It took us a while but we discovered all kinds of places where we could clean up code, disposing of forms, data tables, etc. and then using the GC.Collect

Thanks for your help. This was a tedious process as the only way to test it is to just let it run for hours.
0
CodeCruiserCommented:
Glad its fixed :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.