第一次脚本监测内存泄露记录
作者:网络转载 发布时间:[ 2013/12/26 15:26:15 ] 推荐标签:开发 客户端 操作系统
测试某C++产品B,对发布的API自己写代码封装,每个API循环10000次,查看客户端的程序以及Server端的服务器进程是否有内存泄露。对于某个API C 循环9999999次的时候,在Window Task Manager看到内存不断在增长:
于是利用VBScript写了一段代码来监测Server进程dllhost:
Option Explicit
Dim strComputer, processid, Name, interval
'CONFIGURATIONS
strComputer = "localhost"
'processid = 524
name = "dllhost"
interval = 300000
Dim objFSO, objFolder, objShell, objTextFile, objFile,oArgs,OSCaption,OScanExe,objRefresher,numCount
Dim strDirectory,strFile,logFile,strText,userTime,procTime,interTime,idleTime, dayStr, timeStr
Dim wbemServices,wbemObject,wbemObjectSet
'strDirectory = "."
'strFile = "c: empproc.txt"
'timeStr = replace(Time, ":", ".")
'dayStr = replace(Day, "", ".")
strText =""
logFile = name & "@" & replace(time, ":", ".") & "%" & replace(date, "/", ".") & "@" & strComputer & ".csv"
'WScript.echo logFile
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Check that the strDirectory folder exists
'If objFSO.FolderExists(strDirectory) Then
'Set objFolder = objFSO.GetFolder(strDirectory)
'Else
'Set objFolder = objFSO.CreateFolder(strDirectory)
'WScript.Echo "Just created " & strDirectory
'End If
If objFSO.FileExists(logFile) Then
Set objFile = objFSO.GetFile(logFile)
Else
Set objFile = objFSO.CreateTextFile(logFile)
End If
Set objFile = nothing
set objFolder = nothing
' OpenTextFile Method needs a Const value
' ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForWriting = 2
Set objTextFile = objFSO.OpenTextFile (logFile, ForWriting, True)
'strComputer = "." ' Dot (.) equals local computer in WMI
Set wbemServices = Getobject("winmgmts:\" & strComputer)
Dim pName,pHandle,pProcess,pMem
Dim found, strQuery, wbemObjectSet1, wbemObjectSet2, wbemObject1
do
strQuery = "SELECT * FROM Win32_PerfFormattedData_PerfProc_Process where name LIKE 'dllhost%'"
Set wbemObjectSet = wbemServices.ExecQuery(strQuery, ,48)
'if wbemObjectSet.size = 0 then WScript.Quit
found = 0
For Each wbemObject In wbemObjectSet
'check if the parent is svchost
strQuery = "SELECT * FROM Win32_PerfFormattedData_PerfProc_Process where IDProcess = " & wbemObject.CreatingProcessID
Set wbemObjectSet1 = wbemServices.ExecQuery(strQuery, ,48)
for each wbemObject1 in wbemObjectSet1
if(wbemObject1.name = "svchost") then
'CONFIGURATION ALSO
'here, you can output as you like
'for example,
'strText = strText & ",""" & XXXXXXXX & """"
'here, XXXXXXXX is a filed specified in the file class Win32_PerfFormattedData_PerfProc_Process.txt
strText = """" & Date & " " & time & """"
strText = strText & ",""" & wbemObject.IDProcess & """"
strText = strText & ",""" & round((wbemObject.WorkingSet / 1048576), 1) & """"
strText = strText & ",""" & wbemObject.PercentProcessorTime& """"
strText = strText & ",""" & wbemObject.PercentUserTime& """"
objTextFile.WriteLine(strText)
end if
next
'strText = strText & "Name [" & pName & "] Handle [" & pHandle &"] PID ["&pProcess&"] Mem ["&pMem&"]" & vbCrLf
Next
'strText = strText & "--------------------------------------------------------------------" & vbCrLf
'WScript.echo strText
WScript.Sleep interval
loop
objTextFile.Close
WScript.Quit

sales@spasvo.com