[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2009-03-31
7
Medium Priority
?
3,655 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 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
Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses

656 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