Re: Running Out Of Memory
Every time you call Replace you are creating a new String object so, if you're doing that a lot with large Strings then that would explain why you're running out of memory. You might want to call GC.Collect intermittently to force the system to reclaim the memory occupied by the discarded Strings. You don't want to do it often because it will impact performance so experiment to find out what works best.
Re: Running Out Of Memory
I put 3 GC.Collect() in the middle of all of the Replace's and it still breaks at the same line
Re: Running Out Of Memory
I think we need more information. Show us all the relevant code and give us a more accurate idea of what your files contain.
Re: Running Out Of Memory
Quote:
Originally Posted by
jmcilhinney
I think we need more information. Show us all the relevant code and give us a more accurate idea of what your files contain.
Code:
For Each Dir As String In IO.Directory.GetDirectories(FileLocation)
For Each php As String In IO.Directory.GetFiles(Dir, "*.php")
Dim fileText As String = IO.File.ReadAllText(php)
Dim path As String = php
Dim txt As String = fileText
i = i + 1
Me.BackgroundWorker1.ReportProgress(i, "Working...")
'Followed By Around 11,000 Of these type of lines of code
txt = txt.Replace("Replaced This", "Replace Item")
Re: Running Out Of Memory
11,000!! :eek: No wonder there's a problem. How big are your files? If a file is 1 kilobyte then after processing one file you'll have used 11 megabytes of memory just to store all the strings.
You might try using a StringBuilder instead of a String. I've never actually used StringBuilder.Replace but StringBuilders are generally more efficient than Strings when joining multiple substrings together so I'd guess that they would be when replacing substrings too.