Results 1 to 6 of 6

Thread: Running Out Of Memory

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    84

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,352

    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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    84

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,352

    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.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    84

    Re: Running Out Of Memory

    Quote Originally Posted by jmcilhinney View Post
    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")

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,352

    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
  •  



Click Here to Expand Forum to Full Width