gpt4 book ai didi

memory-leaks - 监控系统性能泄漏内存的VBscript

转载 作者:行者123 更新时间:2023-12-01 11:07:53 24 4
gpt4 key购买 nike

我有一个简单的脚本,可以在 Windows XP 中循环监视进程的不同性能统计信息,直到它终止。
尽管我做出了努力,但脚本的内存占用量会随着时间的推移而增加。
非常感谢任何建议。

    Set fso = CreateObject("Scripting.FileSystemObject")
logFileDirectory = "C:\POSrewrite\data\logs"
Dim output
Dim filePath

filePath = "\SCOPerformance-" & Day(Now()) & Month(Now()) & Year(Now()) & ".log"

IF fso.FolderExists(logFileDirectory) THEN

ELSE
Set objFolder = fso.CreateFolder(logFileDirectory)
END IF

logFilePath = logFileDirectory + filePath + ""

IF (fso.FileExists(logFilePath)) THEN
set logFile = fso.OpenTextFile(logFilePath, 8, True)
output = VBNewLine
output = output & (FormatDateTime(Now()) + " Open log file." & VBNewLine)

ELSE
set logFile = fso.CreateTextFile(logFilePath)
output = output & (FormatDateTime(Now()) + " Create log file." & VBNewLine)
END IF

output = output & (FormatDateTime(Now()) + " Begin Performance Log data." & VBNewLine)
output = output & ( "(Process) (Percent Processor Time) (Working Set(bytes)) (Page Faults Per Second) (PrivateBytes) (PageFileBytes)" & VBNewLine)

WHILE (True)
On Error Resume NEXT
IF Err = 0 THEN

strComputer = "."
Set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
Set objServicesCimv2 = GetObject("winmgmts:\\" _
& strComputer & "\root\cimv2")
Set objRefreshableItem = _
objRefresher.AddEnum(objServicesCimv2 , _
"Win32_PerfFormattedData_PerfProc_Process")
objRefresher.Refresh
' Loop through the processes three times to locate
' and display all the process currently using
' more than 1 % of the process time. Refresh on each pass.

FOR i = 1 TO 3

objRefresher.Refresh
FOR Each Process in objRefreshableItem.ObjectSet
IF Process.PercentProcessorTime > 1 THEN
output = output & (FormatDateTime(Now()) & "," & i ) & _
("," & Process.Name & _
+"," & Process.PercentProcessorTime & "%") & _
("," & Process.WorkingSet) & ("," & Process.PageFaultsPerSec) & _
"," & Process.PrivateBytes & "," & Process.PageFileBytes & VBNewLine
END IF
NEXT
NEXT
ELSE
logFile.WriteLine(FormatDateTime(Now()) + Err.Description)
END IF
logFile.Write(output)
output = Empty
set objRefresher = Nothing
set objServicesCimv2 = Nothing
set objRefreshableItem = Nothing
set objFolder = Nothing
WScript.Sleep(10000)
Wend

最佳答案

我认为您的脚本的主要问题是您在循环内初始化 WMI 对象,也就是说,在循环的每次迭代中,即使这些对象始终相同:

strComputer = "."
Set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
Set objServicesCimv2 = GetObject("winmgmts:\\" _
& strComputer & "\root\cimv2")
Set objRefreshableItem = _
objRefresher.AddEnum(objServicesCimv2 , _
"Win32_PerfFormattedData_PerfProc_Process")

您需要将此代码移出循环,例如,在脚本的开头。


其他提示和建议:

  • 使用 Option Explicit并显式声明脚本中使用的所有变量。已声明的变量比未声明的变量稍快。

  • 使用 FileSystemObject.BuildPath组合路径的多个部分。此方法的有用之处在于它会为您插入必要的路径分隔符。

    logFileDirectory = "C:\POSrewrite\data\logs"
    filePath = "SCOPerformance-" & Day(Now) & Month(Now) & Year(Now) & ".log"
    logFilePath = fso.BuildPath(logFileDirectory, filePath)
  • objFolder 变量未在您的脚本中使用,因此无需创建它。此外,您可以按如下方式重写 FolderExists 检查使其更具可读性:

    If Not fso.FolderExists(logFileDirectory) Then
    fso.CreateFolder logFileDirectory
    End If
  • 将重复的代码移到子例程和函数中以便于维护:

    Function DateTime
    DateTime = FormatDateTime(Now)
    End Function
    ...
    output = output & DateTime & " Open log file." & vbNewLine
  • 通常在连接字符串时不需要括号:

    output = output & DateTime & "," & i & _
    "," & Process.Name & _
    "," & Process.PercentProcessorTime & "%" & _
    "," & Process.WorkingSet & "," & Process.PageFaultsPerSec & _
    "," & Process.PrivateBytes & "," & Process.PageFileBytes & vbNewLine

关于memory-leaks - 监控系统性能泄漏内存的VBscript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3469544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com