Solved

Powershell continous loop memory consumption

Posted on 2016-10-10
6
34 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

14 Experts available now in Live!

Get 1:1 Help Now