title: Start-Sleep memory leak (or causing powershell memory leak)?
body: <p>This question is a repost from <a href="https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/365ca396-400d-4401-bd7f-05d5d6c22cc8/powershells-startsleep-causes-memory-growth?forum=winserverpowershell" rel="nofollow">https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/365ca396-400d-4401-bd7f-05d5d6c22cc8/powershells-startsleep-causes-memory-growth?forum=winserverpowershell</a></p>
  
  <p>I did not get a working answer there so try if this forum can help. This is blocking my work.</p>
  
  <p>Here is my two powershell scripts. The first one is to create an event source and the second one is to generate events using 4 threads. Start-Sleep is used in each thread to control the event generation rate. If I remove the Start-Sleep, then the powershell memory usage is constant, otherwise it grows fast until all system memory is used and the system becomes extremely slow.</p>
  
  <p>Is this a known issue? Any workaround? Appreciate any clue.</p>
  
  <pre><code># use this script to create channel and source if they does not exist.
  
  $logName = "TestLog"
  $sourceName = "TestSource"
  
  New-EventLog -LogName $logName -Source $sourceName
  
  # maximumSize's max is 4G.
  Limit-EventLog -LogName $logName -OverflowAction OverWriteOlder -RetentionDays 30 -MaximumSize 3080000KB
  </code></pre>
  
  <p>Event generating script:</p>
  
  <pre><code># use this script to generate events in TestLog channel.
  
  Param(
    [int]$sleepIntervalInMilliSeconds = 0
  )
  
  $eventGenScript = {
    $logName = "TestLog"
    $sourceName = "TestSource"
  
    while($true) {
      Write-EventLog -LogName $logName -Source $sourceName -Message "perfLog" -EventId 0 -EntryType information
      Start-Sleep -ms $sleepIntervalInMilliSeconds
    }
  }
  
  $threadCount = 4
  
  for($i=0; $i -lt $threadCount; $i++)
  {
    Start-Job $eventGenScript  
  }
  
  read-host "type a key to exit. You need to wait for some time for threads to exit."
  </code></pre>