We have a Web application that seems to periodically crash one of our Web servers. On a few occasions, we've noticed that once the memory used by the application hits 1MB, it quickly spirals upwards and kills the server. Is there an easy way to monitor the performance of a particular process and report the memory consumption growth before the server crashes?
Yes—Sysinternals' PsList utility (part of PsTools at http://www.sysinternals.com) lets you view processes and their details, including memory usage. The results are similar to what you'd see in Task Manager's Processes tab. As the MemoryTest.bat script in Listing 1 shows, you can run PsList, use the Find command to find the name of the process you want to monitor, then use an If command with the GEQ (greater than or equal to) comparison operator to determine whether the threshold you've set for that process has been reached. MemoryTest.bat uses the Net Send command to deliver a Messenger service message to the specified PC. Alternatively, you could adapt the script to use Blat to send an email message instead. (You can find this free Win32 command-line utility at http://www.blat.net.) For information about how to use Blat, see "Real-World Scripting: Using Blat to Send Email Notification Messages," November 2000, InstantDoc ID 15848.
To run MemoryTest.bat, you follow the syntax
MemoryTest.bat Server Process Thresh PopupTarget
where Server is the name of the server on which you want to run MemoryTest.bat, Process is the name of the process you want to monitor, Thresh is the threshold for that process, and PopupTarget is the name of the PC to receive the Messenger service message. To use this script, you need to have the Messenger service enabled on that PC.
LISTING 1: MemoryTest.bat Set Server=%1 Set Process=%2 Set Thresh=%3 Set Popuptarget=%4 Set Mem= For /F "tokens=6" %%i in ('PSlist.exe \\%Server%^| Find /I "%Process% "') Do If %%i GEQ %Thresh% Set Mem=%%i If Defined Mem Goto :popup Goto :EOF :popup Net Send %Popuptarget% "%Date% - %Time% %Server% Memory level on %Process% process is over %Thresh% threshold at %Mem%" Goto :EOF