1

I have a powershell script with a while loop, when itr first start the script its using 15,504kb memory, but it looks like every time it goes through the while loop the memory usage is increasing by about 100kb, and it never seems to go down.

$exe  = 'C:\Program Files (x86)\MyApp\MyApp.exe'
$logOutput = 'C:\Log\Log.log'
$logdir1 = 'C:\Log'
$storage = 'ext'
$date = (Get-Date)
"{0:dd/MM/yy HH:mm:ss}  -  START"  -f $date | Out-File -filepath $logOutput -NoClobber -Append -Encoding ASCII

While ($true){
  $date = (Get-Date)
  "{0:dd/MM/yy HH:mm:ss}  -  Calling exe for D:\" -f $date | Out-File -filepath $logOutput -NoClobber -Append -Encoding ASCII
  & $exe /logdir $logdir1 /storage $storage
  Start-Sleep -s 5

  $date = (Get-Date)
  "{0:dd/MM/yy HH:mm:ss}  -  Calling exe for E:\" -f $date | Out-File -filepath $logOutput -NoClobber -Append -Encoding ASCII
  & $exe /logdir $logdir1 /storage $storage
  Start-Sleep -s 10
}

Is there something wrong with my script?

Jeffrey
  • 2,095
  • 3
  • 20
  • 36
  • 2
    `[System.GC]::Collect()}` might resolve your problem. Check out this [related post](https://stackoverflow.com/questions/31620763/no-garbage-collection-while-powershell-pipeline-is-executing), and the [MSDN on garbage collection](https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/)(Induced Collection [here](https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/induced)) – G42 Feb 11 '18 at 10:45
  • 100kb is TINY, what is the problem you need to solve exactly? Also, `while($true)` and sleeping the thread as you are, to me are code smells. – Austin T French Feb 11 '18 at 22:30
  • 1
    The unknown in your code is "MyApp.exe". It could have a memory leak. Is it creating objects that have a .Dispose() method? If so, call it. – Mike Smith - MCT Feb 11 '18 at 22:38

0 Answers0