Click here to Skip to main content
15,917,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
# Solved I want to make a Windows Form application that show every Windows services and memory usage and want to restart the service that uses a certain amount of memory. No problem with accessing services but no idea how to calculate memory usages.

What I have tried:

I access services with using ServiceController.
Posted
Updated 18-Jul-19 21:30pm
v3
Comments
CHill60 18-Jul-19 4:03am    
How are you "accessing" services? Show your code
Ercan Sormaz 18-Jul-19 4:08am    
ServiceController[] scServices;
scServices = ServiceController.GetServices();
foreach (ServiceController scTemp in scServices)
{

if (scTemp.Status == ServiceControllerStatus.Running)
{
listBox4.Items.Add("Service: " + scTemp.ServiceName).ToString();
listBox5.Items.Add("Display name: " + scTemp.DisplayName).ToString();
}

You can try to use clr profile download from microsoft site. And give the path your exe it will give memory usage.
 
Share this answer
 
Comments
Ercan Sormaz 19-Jul-19 2:12am    
Thank you for your advice.
In the System.Diagnostics namespace use a Process Class[^]. See the properties for memory size

See this link[^] for an example of how to get a Process object from a given ServiceController
 
Share this answer
 
v2
Comments
Ercan Sormaz 18-Jul-19 4:12am    
I can't use Process class and ServiceController together, unfortunately
CHill60 18-Jul-19 4:26am    
Yes you can. See my updated solution for an example
Ercan Sormaz 18-Jul-19 6:05am    
ManagementObject wmiService;
wmiService = new ManagementObject("Win32_Service.Name='" + scTemp.ServiceName + "'");
object o = wmiService.GetPropertyValue("ProcessId");

int processId = (int)((UInt32)o);
Process process = Process.GetProcessById(processId);
listBox1.Items.Add(process.VirtualMemorySize64.ToString());
}

i did this to calculate usage of memory for each service but i take an error.
System.NullReferenceException: 'Object reference not set to an instance of an object.'
CHill60 18-Jul-19 7:14am    
On which line?
Ercan Sormaz 18-Jul-19 7:36am    
int processId = (int)((UInt32)o);
This line.
This link[^] solved my problem. Thank you all.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900