Solved

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

Posted on 2009-03-31
7
3,201 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
[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
  • 3
  • 3
7 Comments
 
LVL 71

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 71

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. 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

A brief introduction to what I consider to be the best editor for PowerShell.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

738 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