Windows 伺服器

Windows 伺服器 CPU 佔用高、記憶體佔用高、磁碟 100% 排查:工作管理員、資源監視器與 PowerShell

6 個步驟 11 分鐘閱讀 18 次閱讀 0
本文目錄

Windows 伺服器執行緩慢、遠端桌面卡死或 CPU 長期佔用 100%,通常是某個程序佔滿了資源、記憶體耗盡或磁碟過於繁忙。本文介紹如何在 Windows Server 2016、2019、2022 和 2025 上用工作管理員、資源監視器和 PowerShell 找出原因,如何區分正常的系統活動與真正的問題,以及如何發現挖礦程式等可疑程式。

在確認清楚之前,不要隨意結束不認識的程序。結束 csrss.exe、lsass.exe、winlogon.exe 等系統程序會導致伺服器崩潰或當前使用者被登出。請先記下程序名、PID 和檔案路徑。

步驟 1:用工作管理員檢視整體情況

按 Ctrl+Shift+Esc 開啟工作管理員(在遠端桌面中請按 Ctrl+Alt+End,再選擇“工作管理員”)。點選詳細資訊(More details),然後在程序(Processes)或詳細資訊(Details)選項卡中按 CPU、記憶體或磁碟排序。在詳細資訊(Details)選項卡中右鍵單擊列標題,選擇選擇列(Select columns)並勾選命令列(Command line),即可看到每個程序實際執行的完整命令。

如需進一步分析,可以開啟資源監視器(Resource Monitor,resmon)。它的 CPU 選項卡會顯示每個 svchost.exe 中執行著哪些服務,磁碟選項卡顯示正在讀寫哪些檔案,網路選項卡則顯示哪個程序正在與哪個遠端地址通訊。

步驟 2:用 PowerShell 找出佔用高的程序

即使桌面卡得難以操作,PowerShell 通常仍然可以正常使用。請以管理員身份執行。

# Overall CPU usage, 5 samples 2 seconds apart
Get-Counter '\Processor(_Total)\% Processor Time' -SampleInterval 2 -MaxSamples 5

# Processes using the most CPU right now (percentage of total CPU)
Get-Counter '\Process(*)\% Processor Time' -ErrorAction SilentlyContinue |
  Select-Object -ExpandProperty CounterSamples |
  Where-Object { $_.InstanceName -notin '_total','idle' } |
  Sort-Object CookedValue -Descending | Select-Object -First 10 InstanceName,
  @{n='CPU%';e={[math]::Round($_.CookedValue / $env:NUMBER_OF_PROCESSORS, 1)}}

# Processes using the most memory
Get-Process | Sort-Object WorkingSet64 -Descending | Select-Object -First 10 Name, Id,
  @{n='RAM(MB)';e={[math]::Round($_.WorkingSet64 / 1MB)}}

# Which services run inside a busy svchost.exe (replace 1234 with its PID)
tasklist /svc /fi "PID eq 1234"

步驟 3:檢查記憶體和磁碟

# Free physical memory and committed memory
Get-CimInstance Win32_OperatingSystem |
  Select-Object @{n='TotalGB';e={[math]::Round($_.TotalVisibleMemorySize/1MB,1)}},
                @{n='FreeGB';e={[math]::Round($_.FreePhysicalMemory/1MB,1)}}

# Disk activity per drive (sustained % Idle Time near 0 means the disk is the bottleneck)
Get-Counter '\LogicalDisk(*)\% Idle Time','\LogicalDisk(*)\Avg. Disk sec/Transfer' -SampleInterval 2 -MaxSamples 3

# Free space per drive
Get-PSDrive -PSProvider FileSystem | Select-Object Name,
  @{n='FreeGB';e={[math]::Round($_.Free/1GB,1)}}, @{n='UsedGB';e={[math]::Round($_.Used/1GB,1)}}

如果“使用中”(In use)的記憶體接近 100%,同時磁碟持續繁忙,說明伺服器正在把記憶體分頁到磁碟:請增加記憶體,或減少伺服器上執行的程式。系統盤空間快滿時同樣會拖慢 Windows,還可能導致無法安裝更新,請參見擴充套件 C 盤。

步驟 4:瞭解哪些情況屬於正常

  • TiWorker.exe / TrustedInstaller.exe:Windows 更新正在安裝或清理元件。安裝更新後 10–60 分鐘內 CPU 佔用高屬於正常現象,等它執行完成即可(參見管理 Windows 更新)。
  • MsMpEng.exe:Microsoft Defender 正在掃描,掃描期間負載較高是正常的。如果某個繁忙的應用程式反覆觸發掃描,請只為它的資料目錄新增範圍儘量小的排除項,而不要直接關閉 Defender。
  • 備用(已快取)記憶體:Windows 會把空閒記憶體用作檔案快取,程式需要時會立即釋放。真正需要關注的只有“使用中”的記憶體。
  • sqlservr.exe、w3wp.exe、java.exe、node.exe:這些是你的資料庫和應用程式。它們佔用高,通常說明訪問量大、存在慢查詢,或應用程式本身存在記憶體洩漏。

步驟 5:排查可疑程式

如果某個程序名稱是隨機字元,或從 C:\Users\Public、C:\ProgramData、臨時資料夾中執行,又或者是沒有數字簽名卻佔滿 CPU 的執行檔,就需要仔細檢查。挖礦程式經常偽裝成 svchost.exe 並放在錯誤的目錄中,而真正的 svchost.exe 只會從 C:\Windows\System32 執行。

# Path, command line and digital signature of a suspicious process (replace 1234)
Get-CimInstance Win32_Process -Filter "ProcessId=1234" | Select-Object Name, ExecutablePath, CommandLine
Get-AuthenticodeSignature (Get-Process -Id 1234).Path | Select-Object Status, SignerCertificate

# svchost.exe copies running from anywhere other than System32 or SysWOW64
Get-CimInstance Win32_Process -Filter "Name='svchost.exe'" |
  Where-Object { $_.ExecutablePath -notmatch 'C:\\Windows\\(System32|SysWOW64)\\' } |
  Select-Object ProcessId, ExecutablePath

# Network connections per process
Get-NetTCPConnection -State Established | Group-Object OwningProcess |
  Sort-Object Count -Descending | Select-Object -First 10 Count, Name

# Persistence: scheduled tasks, auto-start services and startup entries
Get-ScheduledTask | Where-Object { $_.State -ne 'Disabled' -and $_.TaskPath -notlike '\Microsoft\*' } |
  Select-Object TaskName, TaskPath, @{n='Action';e={$_.Actions.Execute}}
Get-CimInstance Win32_Service | Where-Object { $_.StartMode -eq 'Auto' -and $_.PathName -notmatch 'Windows' } |
  Select-Object Name, State, PathName
Get-CimInstance Win32_StartupCommand | Select-Object Name, Command, Location

# Update signatures and run a full Microsoft Defender scan
Update-MpSignature
Start-MpScan -ScanType FullScan
Get-MpThreatDetection

如果確認存在惡意程式,請先刪除對應的計劃任務或服務,再結束程序並刪除其檔案,然後修改所有密碼,並堵住入侵途徑(最常見的是遠端桌面弱密碼):參見 Windows VPS 安全加固和修改遠端桌面埠。如果無法確定是否已清理乾淨,請備份資料後重灌系統。

步驟 6:用效能監視器記錄更長時間的資料

時有時無的問題,更適合通過錄制效能資料來捕捉。執行 perfmon,展開資料收集器集 → 系統(Data Collector Sets → System),右鍵單擊 System Performance,選擇開始(Start)。收集完成後(約一分鐘),在報告 → 系統(Reports → System)下生成的報告會列出 CPU、磁碟和網路佔用最高的程序。

常見問題

CPU 佔用 100%,但工作管理員裡看不到佔用高的程序?

開啟顯示所有使用者的程序(Show processes from all users),並檢視詳細資訊(Details)選項卡,系統服務和其他使用者的程序都會顯示在這裡。有些惡意程式在工作管理員開啟時會自動停止執行,此時可以使用上面的 PowerShell 命令;如果你的產品提供資源監控圖表,也可以在 IMIDC 客戶中心檢視。

只有遠端桌面操作卡頓?

如果 CPU、記憶體和磁碟都正常,原因通常在你和伺服器之間的網路線路上。請參見遠端桌面連線故障排查,並用 ping 和 tracert 測試路由。

什麼時候應該升級配置,而不是繼續最佳化?

如果在正常訪問量下,你自己的應用程式本來就會用掉大部分 CPU 或記憶體,那麼最佳化也只能撐一時。請升級套餐,或把資料庫遷移到另一台伺服器上。

如果按以上步驟操作後問題仍未解決,請提交工單聯絡 IMIDC 7×24 技術支援,並附上伺服器 IP、Windows Server 版本、佔用高的程序名稱,以及工作管理員截圖或 PowerShell 輸出結果,方便工程師快速定位。

這篇文章有幫助嗎?

相關教程