Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. How to get Memory usage size of each services running on Application Server using C#.net

How to get Memory usage size of each services running on Application Server using C#.net

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpsysadminperformancetutorial
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Michael_Davies

    Do you mean you want to write your own program or do you want a tool that will display the information to you?

    V Offline
    V Offline
    ven753
    wrote on last edited by
    #3

    I want to write a program in C# that should display all services memory usage which is running on particular application server using C#.net.

    M 1 Reply Last reply
    0
    • V ven753

      I want to write a program in C# that should display all services memory usage which is running on particular application server using C#.net.

      M Offline
      M Offline
      Michael_Davies
      wrote on last edited by
      #4

      In which case start by looking at WMI, you can get the WMICodeCreator from Miscrosoft site which will let you select WMI Objects, view them and their properties and write the code to do the same that you can cut and paste into your application. For instance:

      using System;
      using System.Management;
      using System.Windows.Forms;

      namespace WMISample
      {
      public class MyWMIQuery
      {
      public static void Main()
      {
      try
      {
      ManagementObjectSearcher searcher =
      new ManagementObjectSearcher("root\\CIMV2",
      "SELECT * FROM Win32_Service");

                  ManagementObjectSearcher procsearcher =
                      new ManagementObjectSearcher("root\\\\CIMV2",
                      "SELECT \* FROM Win32\_Process");
                  String q;
      
                  foreach (ManagementObject queryObj in searcher.Get())
                  {
                      if (queryObj\["ProcessId"\].ToString() != "0")
                      {
                      Console.WriteLine("-----------------------------------");
                                          
                      q = "SELECT \* FROM Win32\_Process WHERE ProcessId=";
                      q += queryObj\["ProcessId"\].ToString();
      
                      procsearcher.Query = new System.Management.ObjectQuery(q);
      
                      foreach (ManagementObject ProcObj  in procsearcher.Get())
                      {
                          Console.WriteLine("Name: {0} : {1}", queryObj\["Name"\], ProcObj\["WorkingSetSize"\]);
                      }
                      }
                  }
              }
              catch (ManagementException e)
              {
                  MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
              }
          }
      }
      

      }

      You can also get the ProcessId which will be non-zero if the service is running, using the ProcessId you can query WMI to get the process information. Updated the code to get the memory for running Services.

      V 1 Reply Last reply
      0
      • M Michael_Davies

        In which case start by looking at WMI, you can get the WMICodeCreator from Miscrosoft site which will let you select WMI Objects, view them and their properties and write the code to do the same that you can cut and paste into your application. For instance:

        using System;
        using System.Management;
        using System.Windows.Forms;

        namespace WMISample
        {
        public class MyWMIQuery
        {
        public static void Main()
        {
        try
        {
        ManagementObjectSearcher searcher =
        new ManagementObjectSearcher("root\\CIMV2",
        "SELECT * FROM Win32_Service");

                    ManagementObjectSearcher procsearcher =
                        new ManagementObjectSearcher("root\\\\CIMV2",
                        "SELECT \* FROM Win32\_Process");
                    String q;
        
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        if (queryObj\["ProcessId"\].ToString() != "0")
                        {
                        Console.WriteLine("-----------------------------------");
                                            
                        q = "SELECT \* FROM Win32\_Process WHERE ProcessId=";
                        q += queryObj\["ProcessId"\].ToString();
        
                        procsearcher.Query = new System.Management.ObjectQuery(q);
        
                        foreach (ManagementObject ProcObj  in procsearcher.Get())
                        {
                            Console.WriteLine("Name: {0} : {1}", queryObj\["Name"\], ProcObj\["WorkingSetSize"\]);
                        }
                        }
                    }
                }
                catch (ManagementException e)
                {
                    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
                }
            }
        }
        

        }

        You can also get the ProcessId which will be non-zero if the service is running, using the ProcessId you can query WMI to get the process information. Updated the code to get the memory for running Services.

        V Offline
        V Offline
        ven753
        wrote on last edited by
        #5

        i just run your code. Some error came (The type or namespace name 'ManagementObjectSearcher' could not be found). i want to get All Services memory usage running on Application Server in local system using C#. How to achieve this. Please reply me.

        M P 2 Replies Last reply
        0
        • V ven753

          i just run your code. Some error came (The type or namespace name 'ManagementObjectSearcher' could not be found). i want to get All Services memory usage running on Application Server in local system using C#. How to achieve this. Please reply me.

          M Offline
          M Offline
          Michael_Davies
          wrote on last edited by
          #6

          Did you add system.management and system.management.instrumentation as references to your project? From Project menu select Add Reference, in the resulting dialogue click the .Net tab and scroll to locate the System.Management and add it to your project, same again for .Instrumentation. FYI. I updated the code in previous reply to get the memory for the process as well.

          V 2 Replies Last reply
          0
          • V ven753

            i just run your code. Some error came (The type or namespace name 'ManagementObjectSearcher' could not be found). i want to get All Services memory usage running on Application Server in local system using C#. How to achieve this. Please reply me.

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #7

            Repeating your question isn't going to get you any faster help. When you encounter an error like this, a quick Google search is enough to find out which DLL you need to reference.

            V 1 Reply Last reply
            0
            • M Michael_Davies

              Did you add system.management and system.management.instrumentation as references to your project? From Project menu select Add Reference, in the resulting dialogue click the .Net tab and scroll to locate the System.Management and add it to your project, same again for .Instrumentation. FYI. I updated the code in previous reply to get the memory for the process as well.

              V Offline
              V Offline
              ven753
              wrote on last edited by
              #8

              Hi, I added the reference and is working. [Workingsetsize] means usage memory size or allocated size and this will be for checking in local system no. If i want to check the memory usage of services which are running in Application server from my local system, how can i achieve. how to specify app server from my local system through this code and get memory usage of services of that app server. Please reply me.

              1 Reply Last reply
              0
              • P Pete OHanlon

                Repeating your question isn't going to get you any faster help. When you encounter an error like this, a quick Google search is enough to find out which DLL you need to reference.

                V Offline
                V Offline
                ven753
                wrote on last edited by
                #9

                I added the reference and is working. [Workingsetsize] means usage memory size or allocated size and this will be for checking in local system no. If i want to check the memory usage of services which are running in Application server from my local system, how can i achieve. how to specify app server from my local system through this code and get memory usage of services of that app server. Please reply me.

                M 1 Reply Last reply
                0
                • M Michael_Davies

                  Did you add system.management and system.management.instrumentation as references to your project? From Project menu select Add Reference, in the resulting dialogue click the .Net tab and scroll to locate the System.Management and add it to your project, same again for .Instrumentation. FYI. I updated the code in previous reply to get the memory for the process as well.

                  V Offline
                  V Offline
                  ven753
                  wrote on last edited by
                  #10

                  Hi, how to specify app server IP and service name (If possible) from my local system through this code and get memory usage of services of that app server. How to achieve this. Please reply me.

                  M 1 Reply Last reply
                  0
                  • V ven753

                    Hi, how to specify app server IP and service name (If possible) from my local system through this code and get memory usage of services of that app server. How to achieve this. Please reply me.

                    M Offline
                    M Offline
                    Michael_Davies
                    wrote on last edited by
                    #11

                    Use Google.

                    1 Reply Last reply
                    0
                    • V ven753

                      I added the reference and is working. [Workingsetsize] means usage memory size or allocated size and this will be for checking in local system no. If i want to check the memory usage of services which are running in Application server from my local system, how can i achieve. how to specify app server from my local system through this code and get memory usage of services of that app server. Please reply me.

                      M Offline
                      M Offline
                      Michael_Davies
                      wrote on last edited by
                      #12

                      Look here [^] Learn to use Google.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups