Solved

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

Posted on 2009-03-31
7
3,291 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
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

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…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

695 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