Solved

Powershell continous loop memory consumption

Posted on 2016-10-10
6
41 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
This article will help you understand what HashTables are and how to use them in PowerShell.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 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