-
Jul 28th, 2009, 10:38 PM
#1
Thread Starter
Lively Member
Running Out Of Memory
OK I am working on a project for a friend. It loads all php files in a specified folder and looks for text in them and if it finds a text it replaces it, then it saves the file and moves on to the next.
so it is like
Code:
txt = txt.Replace("j", "from")
there are around 13,000 lines like this that it searches the files with, it always runs out of memory at the same line.
I need a way to cut down on the memory usage, it is already running on a background worker, but didnt seem to help any.
-
Jul 28th, 2009, 11:04 PM
#2
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.
-
Jul 28th, 2009, 11:11 PM
#3
Thread Starter
Lively Member
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
-
Jul 28th, 2009, 11:40 PM
#4
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.
-
Jul 28th, 2009, 11:46 PM
#5
Thread Starter
Lively Member
Re: Running Out Of Memory
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")
-
Jul 28th, 2009, 11:52 PM
#6
Re: Running Out Of Memory
11,000!! 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|