函数返回后变量的powershell内存空间

函数返回后变量的powershell内存空间,powershell,memory-management,Powershell,Memory Management,这是我的powershell函数: function A { $a = "securestring" return $a } $b = A Remove-Variable -Name b -Scope "Local" $b现在已不在内存中。但是$a呢?简单的答案是:不。当它退出该范围后,它的变量就消失了。此外,如果您希望$B捕获函数的输出,那么实际上应该执行以下操作: Function A { "securestring" } $B = A > $B > sec

这是我的powershell函数:

function A
{
  $a = "securestring"
  return $a
}

$b = A
Remove-Variable -Name b -Scope "Local" 

$b现在已不在内存中。但是$a呢?

简单的答案是:不。当它退出该范围后,它的变量就消失了。此外,如果您希望$B捕获函数的输出,那么实际上应该执行以下操作:

Function A
{
    "securestring"
}
$B = A
> $B
> securestring
> $B.GetType().Name
> String
Remove-Variable -Name B -Scope 'Local'
> $B
> $B.GetType()
> You cannot call a method on a null-valued expression.

您的示例有点复杂。

在函数作用域中运行时也不再在内存中,并且在函数完成运行后将被清除。“$b现在不再在内存中。”-您怎么知道?出现.Net垃圾收集器,因此您无法保证它何时运行。如果单词choice
securestring
提示您需要清除物理内存以实现某些重要的安全目的,那么它超出PowerShell函数的作用域并不能做到这一点。我想垃圾收集和删除变量或清除变量是不同的?