• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

is cleanup necessary with powershell? set var = nothing

J

James

#1
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 Computer

U

Uwe Kausch

#2
Hi James,

how about "Remove-Variable"? For details please check http://technet.microsoft.com/en-us/library/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 Computer

A

Alex K. Angelopoulos

#3
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 Computer

J

James

#4
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 Computer

J

Josh Einstein

#5
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 Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)