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
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
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