Solved

PS 4 memory management

Posted on 2014-01-03
2
161 Views
Last Modified: 2014-01-07
Powershell 3/4
I have a script that is about 900 lines long and reads of record of every in a large database table one record at a time (very complex query).
As the program is running Resource Manager shows the power shell ISE is consuming more and more memory. The program is currently running and the system started of with 2.6GB used memory and is now up to 5.6GB used and slowly growing as the script cycles through each record and does it's processing.

At the top of the script I have:
[System.GC]::Collect()
Which I read would handle al garbage collecting???

I then read others with the same issue get rid of variables and do something like GC::Collect() right afterwards to free the memory, but I have not see any actual examples.

I am not keeping data, just reusing the same variables inside a couple of nested foreach loops. The outer loop cycles through the database tables.
The next loop cycles through each record of whichever table we are on.

Do I need to "destroy" every variable at the end of the foreach loops to keep the memory usage down? How do I destroy a variable?

Please explain because I am on record 10,000 and there's like 2 million more to go!!!
thank you!
0
Comment
Question by:johnj_01201
2 Comments
 
LVL 18

Accepted Solution

by:
Raheman M. Abdul earned 500 total points
ID: 39755221
Inefficient powershell code also consumes more memory.
Foreach code also should be looked at to solve memory issue.
0
 
LVL 1

Author Closing Comment

by:johnj_01201
ID: 39762250
Although not an answer, the only one given. It turns out that the Powershell ISE keeps EVERYTHING that is sent by write-host and that is what was using up the memory. I had the script showing everything it did. I know use CLS in the main foreach loop to clear the output window.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
This article will help you understand what HashTables are and how to use them in PowerShell.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
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…

707 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

13 Experts available now in Live!

Get 1:1 Help Now