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
Registry Cleanup Vista performance & maintenance
Solved Disk Cleanup General Discussion
Trojan cleanup. Vista General
Disk Cleanup Crashes and Debugging
Disk Cleanup Bug Vista performance & maintenance