troubleshooting Question

WMI API causes memory leak

Avatar of peer754
peer754 asked on
Windows XP.NET ProgrammingC#
16 Comments4 Solutions2022 ViewsLast Modified:
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"]);
Christopher Kile
Senior Software Analyst

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 4 Answers and 16 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 4 Answers and 16 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros