powershell中带有COM对象的垃圾回收



我创建了以下函数,用于清理脚本末尾对com对象的所有引用:

function TrashCompactor ($reflist) {
foreach ($ref in $Reflist){

[System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$ref) | out-null
[Runtime.InteropServices.Marshal]::FinalReleaseComObject($ref) | out-null
Remove-Variable $ref | out-null
}

[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}

删除变量会像我预期的那样工作吗?包含[System.GC]::Collect((有什么害处吗?

是,也不是。。。

[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

是一种常见的最佳实践。

Windows总是会进行清理,但它总是在您完成后清理您的环境。

如文件所示。。。

通过跟踪变量使用情况清理PowerShell环境https://devblogs.microsoft.com/scripting/clean-up-your-powershell-environment-by-tracking-variable-use

并涵盖在这个SO问答;一个公认的答案。。。

PowerShell发布COM对象

function Release-Ref ($ref) {
[System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$ref) | out-null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
because I've noted that my comobject always stay alive, I think Powershell 2.0 is not able to remove comobject no more used.
[System.Runtime.InteropServices.Marshal]::ReleaseComObject( $ref )

而SO正是你所问的,所以这个问题实际上是重复的。

在我的例子中,我在变量中使用前缀,这样它们就很容易找到,也很容易全局清理。

# Assign results to a variable and output to the screen using variable squeezing
($ponMyShell = New-Object -com "Wscript.Shell")
($ponDate = Get-Date)
($ponProcess = Get-Process |
Select -First 3)
<#
# Results
Monday, 2 March, 2020 19:40:47
Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName                                                                                                              
-------  ------    -----      -----     ------     --  -- -----------                                                                                                              
186      14     2648       6800       0.14  15336   0 aesm_service                                                                                                             
465      27    24300      34064       0.33  27612  22 ApplicationFrameHost                                                                                                     
158       8     1928       4848       0.02  14268   0 AppVShNotify 
SpecialFolders     CurrentDirectory   
--------------     ----------------   
System.__ComObject C:Windowssystem32
#>
Get-Variable -Name 'pon*'
<#
# Results 
Name                           Value                                                                                                                                               
----                           -----                                                                                                                                               
ponDate                        02-Mar-20 19:46:59                                                                                                                                  
ponMyShell                     System.__ComObject                                                                                                                                  
ponProcess                     {System.Diagnostics.Process (aesm_service), System.Diagnostics.Process (ApplicationFrameHost), System.Diagnostics.Process (AppVShNotify)} 
#>

# Clear resource environment
Get-PSSession |
Remove-PSSession
<#
# Results
#>
[System.Runtime.InteropServices.Marshal]::
ReleaseComObject([System.__ComObject]$ponMyShell) |
Out-Null
<#
# Results
#>
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
<#
# Results
#>
Get-Variable -Name 'pon*' |
ForEach { Get-Variable -Name $_ |
Remove-Variable -Force }
# Validate clean-up
Get-Variable -Name 'pon*'
<#
# Results

#>

相关内容

最新更新