PowerShell script for monitoring Windows Service memory utilization
Recently, we were facing issues with one of our windows service, its memory utilization keep on increasing over time i.e. in goes for few MBs to 2-3 GB in span of 2-3 months of continuous running. For a quick fix (workaround) to address this issue,
- We created a PowerShelll script that:
- Check Windows Service for its current memory utilization
- Restart Windows Service if its memory usage exceeds specified Threshold value
- Create a Task Scheduler that runs PowerShell script on daily basis
In this post, I will be sharing how we can use a simple PowerShell script for monitoring Windows Service memory utilization.
Step 1: Check Windows Service for its current memory utilization
Using Windows Management Interface (WMI)
$ServiceName = "MySampleService" $Service=Get-WmiObject Win32_Service -Filter "name = '$ServiceName'" $ProcessID = $Service.ProcessID $ProcessMem = Get-WmiObject Win32_Process -Filter "ProcessId = '$ProcessID'" $MemSizeInMB = $ProcessMem.WS/1MB write-host $ProcessMem.WS
OR
Using Common Information Model (CIM) – Recommended
$ServiceName = "MySampleService" $Service=Get-CIMInstance Win32_Service -Filter "name = '$ServiceName'" $ProcessID = $Service.ProcessID $ProcessMem = Get-CIMInstance Win32_Process -Filter "ProcessId = '$ProcessID'" $MemSizeInMB = $ProcessMem.WS/1MB write-host $ProcessMem.WS
Here: $ServiceName = Set it to name of the service you want to monitor.
Eg.:
For “Task Scheduler” service, Service Name is Schedule
Get-WmiObject Win32_Service -Filter "name = 'Schedule'"
Get-WmiObject Win32_Process -Filter "ProcessId = '1136'"
Step 2: Restart Windows Service if its memory usage exceeds specified Threshold value
$MemoryThresholdInMB = 2048 If ($MemSizeInMB -gt $MemoryThresholdInMB) { write-host "$ServiceName : $MemSizeInMB" Restart-Service -Name $ServiceName write-host "$ServiceName Restarted" }
Here:
$MemoryThresholdInMB = Memory Threshold in MB
$MemSizeInMB = Service Current Memory Usage (WorkingSet) that we get from Step 1
Step 3: Full sample Powershell script
Windows-Service-Monitoring.ps1 content:
Using Windows Management Interface (WMI)
$ServiceName = "MySampleService" $MemoryThresholdInMB = 2048 $Service=Get-WmiObject Win32_Service -Filter "name = '$ServiceName'" $ProcessID = $Service.ProcessID $ProcessMem = Get-WmiObject Win32_Process -Filter "ProcessId = '$ProcessID'" $MemSizeInMB = $ProcessMem.WS/1MB write-host $ProcessMem.WS write-host "$MemSizeInMB : $MemoryThresholdInMB" If ($MemSizeInMB -gt $MemoryThresholdInMB) { write-host "$ServiceName : $MemSizeInMB" Restart-Service -Name $ServiceName write-host "$ServiceName Restarted" }
OR
Using Common Information Model (CIM) – Recommended
$ServiceName = "MySampleService" $MemoryThresholdInMB = 2048 $Service=Get-CIMInstance Win32_Service -Filter "name = '$ServiceName'" $ProcessID = $Service.ProcessID $ProcessMem = Get-CIMInstance Win32_Process -Filter "ProcessId = '$ProcessID'" $MemSizeInMB = $ProcessMem.WS/1MB write-host $ProcessMem.WS write-host "$MemSizeInMB : $MemoryThresholdInMB" If ($MemSizeInMB -gt $MemoryThresholdInMB) { write-host "$ServiceName : $MemSizeInMB" Restart-Service -Name $ServiceName write-host "$ServiceName Restarted" }
Set $ServiceName & $MemoryThresholdInMB as per your requirements.
Step 3: Use “Windows Task Scheduler” for executing the PowerShell script on daily at a given time.
Here:
Program: powershell
Arguments: -File C:\Windows-Service-Monitoring.ps1
That’s It !!!
.NET Professional | Microsoft Certified Professional | DZone’s Most Valuable Blogger | Web Developer | Author | Blogger
Doctorate in Computer Science and Engineering
Microsoft Certified Professional (MCP) with over 12+ years of software industry experience including development, implementation & deployment of applications in the .NET framework
Experienced and skilled Agile Developer with a strong record of excellent teamwork, successful coding & project management. Specialises in problem identification and proposal of alternative solutions. Provided knowledge and individual mentoring to team members as needed
Among top 3% overall in terms of contribution on Stack Overflow (~2.3 million people reached my posts). Part of the top 1% Stack Overflow answerers in ASP.NET technology.
DZone’s Most Valuable Blogger (MVB)
Created and actively maintain the TechCartNow.com tech blog while also editing, writing, and researching topics for publication.
Excellent skills in Application Development using C#/Vb.Net, .NET Framework, ASP.NET, MVC, ADO.NET, WCF, WPF, Web API, SQL Server, jQuery, Angular, React, BackboneJS