[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-03-31
7
Medium Priority
?
3,804 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 71

Accepted Solution

by:
Chris Dent earned 1200 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 71

Assisted Solution

by:Chris Dent
Chris Dent earned 1200 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
New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

 
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 71

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 800 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month18 days, 4 hours left to enroll

829 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