2014-05-21 21 views
2
$i = 0; 
while($i -lt 100) 
{ 
    $s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri '__echange__uri__' -Authentication Kerberos 

    Import-PSSession $s 

    Get-Module | where { $_.Name -match "tmp" } | Remove-Module 
    Get-PSSession | Remove-PSSession 

    $i = $i + 1 
} 

執行上述腳本後,powershell運行時會佔用超過3.5 GB的內存,而我無法釋放此內存,除非通過查殺整個進程。我懷疑它與Import-PSSession命令有關,可能會發生內存泄漏。有沒有一種方法來釋放由Import-PSSession/New-PSSession分配的內存而不殺死powershell.exe進程?導入PSSession - 內存泄露

我使用Powershell v3.0。

回答

0

調用.NET垃圾收集器應該有所幫助。在的末尾添加[System.GC]::Collect(),而在關閉大括號之前的循環。您也可以嘗試Remove-Variable $s,但在我的環境中沒有任何區別。

一個類似的問題:How to instruct PowerShell to garbage collect .NET objects like XmlSchemaSet?

+0

不幸的是你的解決方案不適合我。 GC.Collect()和Remove-Variable都不能防止內存泄漏。你測試了你的解決方案嗎?你測試了哪些Powershell版本?我使用PS v3.0。 – moody19871987

+0

我在PS4上對它進行了測試,垃圾收集器並未完全阻止泄漏,但它肯定會使增長速度減慢很多因素。 – Raf