Solved

Do I need to "clean up" variables/objects in Powershell?

Posted on 2009-03-31
7
2,887 Views
Last Modified: 2012-05-06
I am just getting started in Powershell scripting.  I have experience in VB and am aware of the importance of "cleaning up" object variables (set objVar = Nothing) when I'm done with them to free up system memory.

My question is, do I need to do the same thing in my Powershell scripts?

For instance, for a line like:
$conn = new-object ('System.Data.SqlClient.SqlConnection')

At some point later should I "Remove-variable" this?
If so, do I only need to do this for object variables?

P.S. I can't believe EE doesn't have a "Powershell" zone.  It's still called "MSH/Monad" here.  They need to fix that.
0
Comment
Question by:KTN-IT
  • 3
  • 3
7 Comments
 
LVL 70

Accepted Solution

by:
Chris Dent earned 300 total points
ID: 24030014

I, alongside others, have requested that the name for the zone be change in the past. Alas the requests tend to go ignored. Just another of the many oddities of zone naming (including things like Active Directory being a child of File Servers).

Anyway...

> My question is, do I need to do the same thing in my Powershell scripts?

It can be beneficial at times, or at least I've found that to be the case. However, no memory will be freed until Garbage Collection runs even after you have run Remove-Variable.

And unless you force it Garbage Collection won't run until it needs to. It should be possible to force it using "[GC]::Collect()" if absolutely necessary.

Whether you need to clean up after yourself or not depends on how long your script is going to run for and how long you keep the underlying (PS) session open.

Chris
0
 
LVL 70

Assisted Solution

by:Chris Dent
Chris Dent earned 300 total points
ID: 24030180

Just as a point of interest...

> I have experience in VB and am aware of the importance of "cleaning up" object variables
> (set objVar = Nothing) when I'm done with them to free up system memory.

This is nowhere near as important as we might think. There are some occasions where setting an object to Nothing is beneficial. But in many cases it simply isn't as they'll get cleaned up when they fall out of scope, then will be subject to Garbage Collection.

I'm as guilty of over-using "Set something = Nothing" as anyone, I tend to do it for every single object I create. Probably 90% of the time it's just clutter ;)

Chris
0
 
LVL 5

Author Comment

by:KTN-IT
ID: 24030238
Thanks, Chris.

I remember you helped me a while back with my server/AD issues, question #23638901.

Powershell is neat.  I can type gv and see all the variables, and remove-variable if I need to.

I'm trying to make a PSH script that runs on a schedule, so the session shouldn't last long at all.  Still, this is more of a question for my education than it is to solve a specific problem.  I want to be able to know how to clean up after myself, whether or not I actually need to.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Author Comment

by:KTN-IT
ID: 24030283
Even if 99% of the time it's clutter, that's important.  What if only 99% of the mail got delivered every day?  What it my code is only 99% correct?  "Close" counts in horseshoes and hand grenades, but not usually in computers...

Just my opinion  :^)
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 24030416

hehe I guess if I were an efficient programmer I would do it only where necessary. However, I'll apply the excuse that I'm a Sys Admin and not a programmer so absolute efficiency is something I'll leave behind.

If I have a really really big object in memory I might set it to $Null, or use Remove-Variable, when I'm done (typically if I'm trying to parse obscenely large log files). I've yet to need to call Garbage Collection explicitly and as the lifetime of the majority of my scripts is very short even that much is more than normally required.

The only times I really need to Remove a variable are where there's danger of using a stale value in a loop. It's something that's quite easy to do in VbScript when looping if insufficient error control or safeguards are in place.

I'd be interested to see what BSonPosh does with his, he's rather better at PowerShell than I :)

Chris
0
 
LVL 18

Assisted Solution

by:BSonPosh
BSonPosh earned 200 total points
ID: 24039138
I normally do not clean much up (unless I know it contains a HUGE amount of data.)

One concern would be Variable count. If you have long running sessions and you are creating tons of variables... you may run into MaximumVariableCount limit (defaults to 4096 I think.) In this case remove-item is your best bet.

You can see MaximumVariableCount via $MaximumVariableCount

That said... variables will be cleaned up with scope. For example... all the variables created by a script will be cleaned up automatically after the script runs (unless you . source the script.)



0
 
LVL 5

Author Closing Comment

by:KTN-IT
ID: 31564862
Thanks for your help!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

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…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 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