Solved

Powershell continous loop memory consumption

Posted on 2016-10-10
6
28 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:mcazabonnet
  • 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 68

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:mcazabonnet
ID: 41837621
Awesome thx!...
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 4

Author Closing Comment

by:mcazabonnet
ID: 41837622
Thx...
0
 
LVL 68

Expert Comment

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

Author Comment

by:mcazabonnet
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now