Possibly Memory Leak - Please Advise...

DeanJ

New member
Joined
Jun 24, 2008
Messages
1
Programming Experience
1-3
Below is my code, currently this is in a form for testing but will be in a windows service when i am done and will execute every 5 minutes....

What this code does is pick up text files that contain test data from 1 directory, it then parses out the data loads it into our Database and then moves the files to an archive directory. What i find though is the program starts out using 16mb of memory, after running it sits at 27mb and doesnt drop, my fear is that when i move this to a service that runs every 5 minutes its going to continue to increase the memory usage and never go back to normal... I have tried to set everything to = nothing as well as .close() and .dispose() all objects but with no avail... please advise....

VB.NET:
Imports System.IO
Imports System.Text
Imports Microsoft.Win32
Imports Oracle.DataAccess.Client

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim intCounter As Int16 = 0
        Dim intRecordResponse As Int16 = 0
        Dim strFileName As String = ""
        Dim strFileContent(10000) As String
        Dim strTemp As String = ""
        Dim strNotes As String = ""
        Dim strInboundDIR As String = "C:\Nokia\Files"
        Dim strOutboundDIR As String = "C:\Nokia\Files\Archive"
        Dim objReader As System.IO.StreamReader
        Dim objRegKey As Microsoft.Win32.RegistryKey
        Dim objOraConnection As New Oracle.DataAccess.Client.OracleConnection()
        Dim objOraCommand As New Oracle.DataAccess.Client.OracleCommand()

        evlMain.Source = "NokiaTARs"

        'Create registry key
        objRegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)
        objRegKey.CreateSubKey("NokiaTARs")
        objRegKey = Registry.LocalMachine.OpenSubKey("Software\NokiaTARs", True)

        '############################################################################################
        '# Takes files from strOutboundDir loads them into the database and moves to strInboundDir. #
        '############################################################################################
        Try
            If objOraConnection.State <> ConnectionState.Open Then
                objOraConnection.ConnectionString = "Data Source=RNRSTG;User ID=Username;password=Password"
                objOraConnection.Open()
            End If
        Catch Ex As Exception
            evlMain.WriteEntry(Ex.Message)
            Exit Sub
        End Try
        For Each strFileName In Directory.GetFiles(strInboundDIR, "*.*", SearchOption.TopDirectoryOnly)
            If System.IO.File.Exists(strFileName) = True Then
                strFileName = Mid(strFileName, Len(strInboundDIR) + 2, 99)
                objReader = File.OpenText(strInboundDIR & "\" & strFileName)
                intCounter = 0
                While Not objReader.EndOfStream
                    strTemp = objReader.ReadLine()
                    strFileContent(intCounter) = Mid(strTemp.Trim, 2, 100)
                    strFileContent(intCounter) = Replace(strFileContent(intCounter), "'", "")
                    If intCounter > 12 Then
                        strNotes = strNotes & strFileContent(intCounter) & Chr(13)
                    End If
                    strNotes = Mid(strNotes, 1, 4000)
                    intCounter += 1
                End While
                Try
                    objOraCommand.Connection = objOraConnection
                    objOraCommand.CommandText = "INSERT INTO DEANJ.JGL_NOKIA_TARS VALUES (NULL,'" & strFileContent(0) & "','" & strFileContent(1) & "'," & strFileContent(2) & ",'" & strFileContent(3) & "','" & strFileContent(6) & "','" & strFileContent(4) & "','" & strFileContent(7) & "','" & strFileContent(5) & "','" & strFileContent(9) & "','" & strFileContent(8) & "'," & strFileContent(10) & ",TO_DATE('" & strFileContent(11) & "','MM/DD/YYYY HH24:MI:SS'),TO_DATE('" & strFileContent(12) & "','MM/DD/YYYY HH24:MI:SS'),SYSDATE,'" & strFileContent(13) & "','" & strNotes & "')"
                    intRecordResponse = objOraCommand.ExecuteNonQuery()
                Catch Ex As Exception
                    evlMain.WriteEntry(Ex.Message)
                    Continue For
                End Try
                'TextBox1.Text = TextBox1.Text & strFileName & vbCrLf
                objReader.Close()
                If System.IO.File.Exists(strOutboundDIR & "\" & strFileName) = True Then
                    System.IO.File.Delete(strOutboundDIR & "\" & strFileName)
                    System.IO.File.Copy(strInboundDIR & "\" & strFileName, strOutboundDIR & "\" & strFileName)
                    System.IO.File.Delete(strInboundDIR & "\" & strFileName)
                Else
                    System.IO.File.Copy(strInboundDIR & "\" & strFileName, strOutboundDIR & "\" & strFileName)
                    System.IO.File.Delete(strInboundDIR & "\" & strFileName)
                End If
            End If
        Next
        strFileName = ""
        Array.Clear(strFileContent, 0, UBound(strFileContent) - 1)
        strTemp = ""
        strNotes = ""
        strInboundDIR = ""
        strOutboundDIR = ""
        objOraCommand.Dispose()
        objOraCommand = Nothing
        objOraConnection.Close()
        objOraConnection.Dispose()
        objOraConnection = Nothing
        objReader.Dispose()
        objReader = Nothing
        'Populate registry key with run time.
        objRegKey.SetValue("LAST_RUN", DateTime.Now)
        objRegKey.Close()
    End Sub
End Class
 
Dynamic memory allocations are normal, especially for visual forms. I see one leak from a quick browse of the code though, you OpenSubKey twice but only close the last key.
 
this would be a great candidate for FIleSystemWatcher so that it only responds to files appearing as and when they appear. Dont forget to include a routine that write to establish exclusive access to the file, inside a try catch, inside a loop - that way you only process the file when it has finished being written
 
Back
Top