系统进程中无效句柄过多

nku*_*ula 5 windows memory-leak handle-leak

我在系统进程 (Windows Server 2008R2 x64) 中遇到了无效句柄的增加。金额约为每周 1,000,000。

根据 Process Explorer,句柄类型是文件。从任务管理器看来,内存并未分配给任何进程,但图形显示高(且不断增长)的物理内存使用率。

如何避免或释放无效的系统句柄?

nku*_*ula 2

我还无法找到根本原因,但我想出了如何清理它。

当我复制其中一个文件进行分析时,我发现无效句柄被“重用”或“刷新”并正确关闭。似乎对文件的操作(如打开、复制、删除)修复了句柄。因此,我创建了 powershell 脚本,首先使用 util Handle v3.51获取句柄列表并打开受影响的文件。第一次运行后,句柄数量减少,物理内存使用量也开始减少,经过几次运行后,情况看起来还不错。清理工作安排在每晚。

$handlesLog = .\handle.exe -p 4  # 4 is System process id

foreach ($line in $handlesLog)
{   
    if ($line -match "<here is the pattern of affected  files>")
    {
        $fileToCopy = <full path to the file>

        if ([System.IO.File]::Exists($fileToCopy))
        {
            try
            {
              $fileStr = New-Object  System.IO.FileStream($fileToCopy,[System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)
            }
            finally
            {
               $fileStr.Close()
               $fileStr.Dispose()
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)