Hey guys,

I wrote a vb .net Windows application, and it rewrites IIS log files on a schedule.

I am containing the re-written log file in a string and then it writes to disk and loops. The log files can be upwards of 500MB.

When running it, i see the CPU go to 100%, and the memory is about 500mb. The thing is, the server this runs on has 6gb of memory.

When the log files reach a certain size, i get out of memory exceptions. I think im missing something here, like clearing the string and dumping to the GC.. Can someone show me where in my code is causing this?

Dim _24HoursAgo As Date = Date.Now.AddHours(-168)
For Each file1 As IO.FileInfo In (New IO.DirectoryInfo("D:\Logs\Nissan\")).GetFiles("*.*")
If file1.LastWriteTime >= _24HoursAgo Then

Dim strfile As String = file1.Name
Dim reader As StreamReader = File.OpenText("D:\Logs\N2\" & strfile)
Dim contents As String = reader.ReadToEnd()
Dim ds As New DataSet
ds.ReadXml("d:\Logs\n.xml")

Dim sb As New StringBuilder(contents)
For Each ro As DataRow In ds.Tables("row").Rows
If ro("find").ToString().Length = ro("count") Then
sb.Replace(ro("find").ToString(), ro("repl").ToString())
End If
Next ro


Dim output As String
output = sb.ToString

Dim objwriter As New StreamWriter("D:\Logs\N\" & strfile)
'Const OPEN_FILE_FOR_APPENDING = 8

' generate a filename base on the script name
Dim strOutputFile2 = "D:\Logs\N\" & strfile

Dim objFileSystem2 = CreateObject("Scripting.fileSystemObject")
Dim objOutputFile2 = objFileSystem2.OpenTextFile(strOutputFile2)
'objwriter.Flush()

objwriter.WriteLine(sb.ToString)
objOutputFile2.Close()

objFileSystem2 = Nothing
objOutputFile2 = Nothing
sb = Nothing

reader.Close()
objwriter.Close()

End If

Next file1

Thanks so much!