Solved

PS 4 memory management

Posted on 2014-01-03
2
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 19

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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
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…

735 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