Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I destroy objects like an ArrayList?

6,047 views
Skip to first unread message

ioioio322

unread,
Jul 10, 2009, 4:53:23 PM7/10/09
to

Using Powershell v1 atm...

I'm working on a large complex script and need to be able to destroy
objects, specifically ArrayLists. How can I do so?

This is how I create ArrayLists in Powershell:
$arrayList = New-Object System.Collections.Arraylist

How do I destroy it?


--
ioioio322

Mike Pfeiffer

unread,
Jul 10, 2009, 5:19:02 PM7/10/09
to
Remove-Variable arrayList

ioioio322

unread,
Jul 11, 2009, 7:40:26 PM7/11/09
to

Thank You. It doesn't work.

Is this for Powershell v2.0? I am using Powershell v1.0.

Are you sure you can use this to delete an OBJECT? An ArrayList is an
object... not a variable (a primitive)...


--
ioioio322

Joel Bennett

unread,
Jul 12, 2009, 1:00:04 AM7/12/09
to
Sorry if this is too basic, but ...

In .Net (and therefore, in PowerShell), you don't really "Destroy"
things unless they are using "real" resources like file handles...

In the case where you have such an object, it implements IDisposable,
which means you can get rid of if by calling $foo.Dispose()

An ArrayList, however, is not disposable.

.Net (and therefore, PowerShell) has a garbage collector -- this means
that anything to which there are no more references will be deleted for
real. The usual simple way of removing references to things is to just
null out the variable, eg:

$list = New-Object System.Collections.ArrayList
$list = $null

Obviously that doesn't actually get rid of the ArrayList, just your
pointer to it (particularly if in between, you made another variable
point to it, or set it as the property of an object).

Remove-Variable simply deletes a variable and its value from the scope,
so it basically does the same thing as setting it to null, except that
the variable will also no longer show up if you list the contents of the
Variable:\ drive. In either case, the next time the garbage collector
runs, the memory will be collected and returned to the system.

If this doesn't help, you're going to have to be more explicit about
what the actual symptoms are that you're trying to cure ;)

--
Joel "Jaykul" Bennett

0 new messages