Windows Vista Forums

is cleanup necessary with powershell? set var = nothing

  1. #1


    James Guest

    is cleanup necessary with powershell? set var = nothing

    Hello,

    coming from vbscript where it was good practice to 'clean up', as in:

    set myVar = CreateObject("someObject")
    ....
    ....
    then when done:
    set myVar = Nothing

    which would free up that memory. Now, powershell is .net under the hood, so
    is this clean up still necessary? or does .net garbage collection handle
    everything?


      My System SpecsSystem Spec

  2. #2


    Uwe Kausch Guest

    Re: is cleanup necessary with powershell? set var = nothing

    Hi James,

    how about "Remove-Variable"? For details please check http://technet.microsoft.com/en-us/l.../dd347612.aspx

    Regards from munich,
    Uwe



    James wrote:

    > Hello,
    >
    > coming from vbscript where it was good practice to 'clean up', as in:
    >
    > set myVar = CreateObject("someObject")
    > ...
    > ...
    > then when done:
    > set myVar = Nothing
    >
    > which would free up that memory. Now, powershell is .net under the hood,
    > so is this clean up still necessary? or does .net garbage collection
    > handle everything?

      My System SpecsSystem Spec

  3. #3


    Alex K. Angelopoulos Guest

    Re: is cleanup necessary with powershell? set var = nothing

    It's useful and necessary in the same situations as it was in VBScript.
    Cleanup in VBScript happened anyway for scoped data - for example, objects
    and other data used within functions automatically went away after the
    function went out of scope. The same thing happens in PowerShell when you're
    using functions and scripts; whether or not you explicitly discard data in a
    subscope, unused references and variables get tossed and are eventually
    garbage-collected.

    So generally, it doesn't matter. The one place that it _can_ be a problem is
    if you're using PowerShell interactively and start collecting large amounts
    of output into variables. Those can suck down memory fairly fast. However,
    it generally doesn't seem to show up as a problem. I believe that's because
    most people don't bother with collecting data into variables during
    interactive sessions. Instead they may just run a command and see the
    output - in which case data goes away immediately - or they may use the
    pipeline to process large data sets item-by-item. In the latter case, you
    generally never get a large bulge of in-memory stored data.

    "James" <noone@xxxxxx> wrote in message
    news:eoq9VmmyJHA.1372@xxxxxx

    > Hello,
    >
    > coming from vbscript where it was good practice to 'clean up', as in:
    >
    > set myVar = CreateObject("someObject")
    > ...
    > ...
    > then when done:
    > set myVar = Nothing
    >
    > which would free up that memory. Now, powershell is .net under the hood,
    > so is this clean up still necessary? or does .net garbage collection
    > handle everything?

      My System SpecsSystem Spec

  4. #4


    James Guest

    Re: is cleanup necessary with powershell? set var = nothing

    thank you for the input, I appreciate it.

    "Alex K. Angelopoulos" <aka(at)mvps.org> wrote in message
    news:%23imMfDoyJHA.5156@xxxxxx

    > It's useful and necessary in the same situations as it was in VBScript.
    > Cleanup in VBScript happened anyway for scoped data - for example, objects
    > and other data used within functions automatically went away after the
    > function went out of scope. The same thing happens in PowerShell when
    > you're using functions and scripts; whether or not you explicitly discard
    > data in a subscope, unused references and variables get tossed and are
    > eventually garbage-collected.
    >
    > So generally, it doesn't matter. The one place that it _can_ be a problem
    > is if you're using PowerShell interactively and start collecting large
    > amounts of output into variables. Those can suck down memory fairly fast.
    > However, it generally doesn't seem to show up as a problem. I believe
    > that's because most people don't bother with collecting data into
    > variables during interactive sessions. Instead they may just run a command
    > and see the output - in which case data goes away immediately - or they
    > may use the pipeline to process large data sets item-by-item. In the
    > latter case, you generally never get a large bulge of in-memory stored
    > data.
    >
    > "James" <noone@xxxxxx> wrote in message
    > news:eoq9VmmyJHA.1372@xxxxxx

    >> Hello,
    >>
    >> coming from vbscript where it was good practice to 'clean up', as in:
    >>
    >> set myVar = CreateObject("someObject")
    >> ...
    >> ...
    >> then when done:
    >> set myVar = Nothing
    >>
    >> which would free up that memory. Now, powershell is .net under the hood,
    >> so is this clean up still necessary? or does .net garbage collection
    >> handle everything?
    >

      My System SpecsSystem Spec

  5. #5


    Josh Einstein Guest

    Re: is cleanup necessary with powershell? set var = nothing

    And furthermore, in .NET the idea of explicit disposal is strongly
    recommended for some objects.

    try {
    $Sql = New-object System.Data.SqlClient.SqlConnection
    Write-Host "`$Sql -is [IDisposable]: $($Sql -is [IDisposable])"
    }
    finally {
    $Sql.Dispose()
    }

    Usually when an object has a Dispose method, calling it is a good idea so
    you don't run out of resources. Even setting the variable to null will cause
    it to hang around for a while until the garbage collector gets to it.

    "James" <noone@xxxxxx> wrote in message
    news:eoq9VmmyJHA.1372@xxxxxx

    > Hello,
    >
    > coming from vbscript where it was good practice to 'clean up', as in:
    >
    > set myVar = CreateObject("someObject")
    > ...
    > ...
    > then when done:
    > set myVar = Nothing
    >
    > which would free up that memory. Now, powershell is .net under the hood,
    > so is this clean up still necessary? or does .net garbage collection
    > handle everything?

      My System SpecsSystem Spec

is cleanup necessary with powershell? set var = nothing

Similar Threads
Thread Forum
Disk Cleanup
Hello I just checked disk cleanup and deleted a few but I need to know If I should also cleanup these 2. Per User Archived Windows error report...
General Discussion
Disk Cleanup ?
First off, I am no computer wiz, so please keep it as simple as possible. I ran Disk Cleanup and found several large items that I was wondering...
General Discussion
Disk Cleanup
I used Disk Cleanup yesterday to clean all files on Drive: "C". It cleaned out all the files on my "J" & "K" Flashdrives which were plugged in at...
General Discussion
Disk Cleanup
How to Use Disk Cleanup in Vista Disk Cleanup finds and then removes temporary files on your computer that you decide you no longer need to...
Tutorials
Disc Cleanup?
What items should be checked?
General Discussion
Cleanup hangs
Hi I have Vista Ultimate, when I run disk cleanup, either through cleanmgr or drive properties it gets to temporary files then goes nowhere,...
General Discussion
Cleanup after SP1 -- Using recovery DVD
Hi, I have been running with SP1 for a while and after a few glitches the reliability score has now reached 10! That's terrific! But my hard drive...
Vista General