by Steve Schofield
This article shows how to retrieve Physical and Virtual memory usage on a specific process using WMI. Most administrators are familiar with viewing information in task manager. One of the most common things I look for are memory settings. Note: the example below shows the Physical memory usage for the ‘sqlserver.exe’ process at 43,716 K and Virtual memory size of 55,376 K. This is a ‘moment in time’ setting and can change often.
Using this code sample can help query a remote machine on a periodic basis to identify offending processes. I use this technique to monitor ‘W3WP.exe’ processes on shared web servers to identify high memory application pools. The code sample returns all the information in the Win32_Process class regarding just the ‘W3WP.exe’ process. You could do this for other processes too. The one additional function also returns the ‘owner’ of the thread which I found required an additional step to query with the ‘thread handle’ The other feature in the component is the ability to pass custom user id and password to the remote machine. This can be handy to not assume the user id credentials the process is running under. Happy WMI coding!
Imports System.Management Imports System.Data.SqlClient Public Class GetStats Sub GetProcessInfo() Dim scope As New ManagementScope(“\” & _ComputerName & “rootcimv2”, options) Try
Function GetOwnerObject(ByVal strHandle As String) As Object Dim s As New ManagementScope( _ Dim opt As ObjectGetOptions Dim o As ManagementObject ‘ Execute the method and obtain the return values. #Region “Properties” Public Property ComputerName() As String Public Property UserName() As String Public Property Password() As String Public Property SelectQuery() As String
#Region “Logging info” Sub ErrorHandler(ByVal exp As Exception) Try If (Not EventLog.SourceExists(LogName)) Then Log.Source = LogName Log.WriteEntry(Message) Sub LogInfo(ByVal strValue As String) Try If (Not EventLog.SourceExists(LogName)) Then Log.Source = LogName Log.WriteEntry(Message) #End Region End Class
|