We help IT Professionals succeed at work.
Get Started

WMI API causes memory leak

peer754 asked
Last Modified: 2012-07-24
I'm developing a small in-house application to monitor the performance such as CPU usage, available memory, disk spaceour etc. I start with a small test settig up the CPU usage monitoring.
I am reading the CPU% once per second using WMI API function and then produce an average value each minute.
The problem with this is I get some memory leakage while looking at the process in the Taskmanager.
I've been googling this but all I found was a hotfix concerning leak in Win Server 2008 while I'm on XP/Win Server 2003 machines.
So, there's anyone here that could provide a working solution based on my code input, I'd be more than pleased!


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Management;
using System.Configuration;
using System.Threading;

namespace ServerMonitoringConsole
    class Program
        static string clientName = "myName";
        static string clientPwd = "myPwd";
        static string domain = "ntlmdomain:myDomain";
        static List<int> cpuLoads = new List<int>();
        static string errorStr = "";

        static void Main(string[] args)
            int load = 0;
            int avg = 0;
            long mem = 0;
            int cpuTimePeriod = 60;
            string[] stringServers = { "serv1", "serv2" };

            ConnectionOptions co = new ConnectionOptions();
            co.Username = clientName;
            co.Password = clientPwd;
            co.Authority = domain;
            List<ManagementScope> msList = new List<ManagementScope>();
            msList.Add(new ManagementScope(@"\\" + stringServers[0] + @"\root\cimv2", co));
            msList.Add(new ManagementScope(@"\\" + stringServers[1] + @"\root\cimv2", co));
            ObjectQuery sq = new ObjectQuery("SELECT * FROM Win32_Processor");
            List<ManagementObjectSearcher> mosList = new List<ManagementObjectSearcher>();

            foreach (ManagementScope ms in msList)
                mosList.Add(new ManagementObjectSearcher(ms, sq));
            while (true)
                load = 0;
                int counter = 0;
                while (cpuLoads.Count <= cpuTimePeriod)
                    foreach (string server in stringServers)
                        GetCPUPerformance(server, ref load, mosList[counter]);
                avg = (int)cpuLoads.Sum() / cpuTimePeriod;


        static void GetCPUPerformance(string computerName, ref int cpuLoad, ManagementObjectSearcher mos)
            using (ManagementObjectCollection moObjs = mos.Get())
                foreach (ManagementObject mo in moObjs)
                        cpuLoad = Convert.ToInt32(mo["LoadPercentage"]);

Open in new window

Watch Question
Senior Software Analyst
This problem has been solved!
Unlock 4 Answers and 16 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE