PowerShell 内存泄漏误解

PowerShell Memory leak misunderstanding

PowerShell 新手,边做边学。

我创建的进程有效,但它最终会锁定我的机器直到它完成,耗尽所有内存。我以为我通过强制垃圾收集器解决了这个问题,并且还从 for-each 语句移动到使用 %() 循环遍历所有内容。

过程简要说明:需要将多个 SharePoint 日志文件合并为单个文件,以跟踪公司所有不同 SharePoint 站点的使用情况。 PowerShell 遍历 SP 服务器上的所有日志目录,并检查目录中的每个文件是否已存在于我的本地计算机上。如果它确实存在,它会附加文件文本,否则它会直接复制。对 SharePoint 日志服务器上的每个文件和目录重复冲洗。在每个循环之间,我强制执行 GC,因为...嗯,因为我的基本理解是循环变量保存在内存中,我想刷新它们。我可能看错了。所以这是有问题的脚本。

$FinFiles = 'F:\Monthly Logging\Logs'

dir -path '\SP-Log-Server\Log-Directory' | ?{$_.PSISContainer} | %{
    $CurrentDir = $_
    dir $CurrentDir.FullName | ?(-not $_.PSISContainer} | %{
        if($_.Extension -eq ".log"){
            $DestinationFile = $FinFiles + '\' + $_.Name
            if((Test-Path $DestinationFile) -eq $false){
                New-Item -ItemType file -path $DestinationFile -Force
                Copy-Item $_.FullName $DestinationFile
            }
            else{
                $A = Get-Content $_.FullName ; Add-Content $DestinationFile $A
                Write-Host "Log File"$_.FullName"merged."
            }
        [GC]::Collect()
    }
    [GC]::Collect()
}

允许 completed/appended 日志文件变得非常非常大(最小 300 MB,最大 1GB)。我不是在关闭我应该关闭的东西,还是在记忆中保持打开的东西? (它目前占我 8 Gig 内存总量的 7.5。)

提前致谢。

您可能会发现 this and this 有帮助。

简而言之:当您需要处理大量数据或 I/O 操作时,Add-Content、Get-Content 和 Out-File 很方便,但速度非常慢。在像您这样的情况下,您想回退到 StreamReader and StreamWriter .NET 类 以获得性能 and/or 内存使用优化。

代码示例:

$sInFile = "infile.txt"
$sOutFile = "outfile.txt"

$oStreamReader = New-Object -TypeName System.IO.StreamReader -ArgumentList @($sInFile)
# $true sets append mode.
$oStreamWriter = New-Object -TypeName System.IO.StreamWriter -ArgumentList @($sOutFile, $true)

foreach ($sLine in $oStreamReader.ReadLine()) {
    $oStreamWriter.WriteLine($sLine)
}

$oStreamReader.Close()
$oStreamWriter.Close()

不要嵌套 Get-ChildItem 这样的命令。请改用通配符。尝试: dir "\SP-Log-Server\Log-Directory\*\*.log" 代替。这应该改善事情的开始。然后将其移至 ForEach($X in $Y){} 循环而不是 ForEach-Object{} 循环(您现在正在使用的循环)。我打赌这会解决您的问题。

所以,重写就在我的脑海中:

$FinFiles = 'F:\Monthly Logging\Logs'

ForEach($LogFile in (dir -path '\SP-Log-Server\Log-Directory\*\*.log')){
    $DestinationFile = $FinFiles + '\' + $LogFile.Name
        if((Test-Path $DestinationFile) -eq $false){
            New-Item -ItemType file -path $DestinationFile -Force
            Copy-Item $LogFile.FullName $DestinationFile
        }
        else{
            $A = Get-Content $LogFile.FullName ; Add-Content $DestinationFile $A
            Write-Host "Log File"$LogFile.FullName"merged."
        }
    }
}

编辑:哦,对了,Alexander Obersht 可能也很对。您也可以从 StreamReader 方法中受益。至少您应该使用 Get-Content-readcount 参数,并且没有理由将其保存为变量,只需将其直接传递给 add-content cmdlet。

Get-Content $LogFile.FullName -ReadCount 5000| Add-Content $DestinationFile

为了进一步解释我的回答,如果您在管道中使用 ForEach-Object,它会将所有内容保存在内存中(无论您的 GC 调用如何)。使用 ForEach 循环不会这样做,应该解决您的问题。