5

When running this script from Powershell or Batch: find and replace characters in Windows XP (Powershell 1.0) all goes well. But after script has stopped processing a text file, Powershell still holds over 1 000 000 kb memory (when inspected from taskmanager) and it does not seem to release the memory usage. I'll have to kill the Powershell process to free up the memory. How to prevent this huge memory usage?

Community
  • 1
  • 1
jjoras
  • 2,001
  • 5
  • 18
  • 16
  • 1
    FWIW: You really shouldn't use the () around the Get-Content unless you absolutely have to edit the file in place -- it causes all the lines to be collected before replacing, which is going to be really expensive with large files – Jaykul Nov 09 '10 at 17:24
  • Thanks, that had a huge effect! – jjoras Nov 09 '10 at 17:45

2 Answers2

12

Try

 [GC]::Collect()

That enforces garbage collection

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
2

It seems that there is a memory leak in Get-Content.

[GC]::Collect didn't work in our case when loading ~6000 documents and we had to use a StreamReader instead:

$sr = New-Object System.IO.StreamReader($filepath)

...

$sr.Dispose()
79E09796
  • 2,120
  • 1
  • 20
  • 33