Solved

VB.NET OutOfMemoryException Error

Posted on 2014-10-23
9
300 Views
Last Modified: 2014-11-09
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
0
Comment
Question by:charlieb01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 40
ID: 40400879
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
 

Author Comment

by:charlieb01
ID: 40400983
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
 
LVL 40
ID: 40401157
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:charlieb01
ID: 40410468
Sorry for the delay in response - got sidetracked. Adding the delay didn't help at all
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 40411808
Try adding

inStream = Nothing
GC.Collect


as the last two lines in your sub.
0
 
LVL 28

Expert Comment

by:Ark
ID: 40417970
Why are you declaring serverStream twice - at module level and at sub level?
0
 
LVL 28

Expert Comment

by:Ark
ID: 40418011
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
 

Author Closing Comment

by:charlieb01
ID: 40431131
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40431314
Glad its fixed :-)
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question