Solved

Powershell continous loop memory consumption

Posted on 2016-10-10
6
39 Views
Last Modified: 2016-10-11
Hi I'm running a continuous loop, and running into a memory issue

$date=(get-date).day
while($date -eq ((get-date).day))
{
   
    Start-Sleep -Seconds 10
   

}

memory consumption of powershell.exe increases over time is there a way to keep it in check for this simple loop?
0
Comment
Question by:Mauro Cazabonnet
  • 3
  • 2
6 Comments
 
LVL 12

Accepted Solution

by:
Dustin Saunders earned 500 total points
ID: 41837427
You can try adding in garbage collection to your loop and see if it will free up the memory.  It's a .NET class you could call every 100 iterations (or so).

$i = 0
$date=(get-date).day
while($date -eq ((get-date).day))
{
    Start-Sleep -Seconds 10
    
    Write-Host OK...
    if ($i -eq 100)
    {
        [System.GC]::Collect()
        $i = 0
    }
    $i++
}

Open in new window

1
 
LVL 69

Expert Comment

by:Qlemo
ID: 41837577
Do you really run only this simple loop? Certainly not - and then the other stuff might be the culprit.
If all you do the loop for is to wait, then you should just run a single sleep with the correct number of seconds.
0
 
LVL 4

Author Comment

by:Mauro Cazabonnet
ID: 41837621
Awesome thx!...
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 4

Author Closing Comment

by:Mauro Cazabonnet
ID: 41837622
Thx...
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 41837937
Does that really help?
0
 
LVL 4

Author Comment

by:Mauro Cazabonnet
ID: 41838265
i ran this simple loop and the [GC.System]::Collect does prevent the powershell.exe thread from increasing in memory. (You can test yourself)
I have it running every 15 minutes on my current service
I do have a complex process running based on user demand, and the working memory on the iis server is not increasing in size
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question