Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8330651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:11:07+00:00 2026-06-09T02:11:07+00:00

My question is similar to Periodical WMI Query Causes Memory Leak? but with threads.

  • 0

My question is similar to Periodical WMI Query Causes Memory Leak? but with threads.

I am writing a simple application to monitor process and memory information from a number of servers. However there is a memory leak. I have whittled down the problem to the following simple Console application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Threading;


namespace ConsoleApplication1
{
    class Program
    {
        public static void dummyQuery(string ip, string query)
        {
            ConnectionOptions connOptions = new ConnectionOptions();
            ManagementScope mgtScope = new ManagementScope(@"\\" + ip + @"\ROOT\CIMV2", connOptions);


            mgtScope.Connect();

            ObjectQuery queryo = new ObjectQuery(query);

            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(mgtScope, queryo))
            {

                using (ManagementObjectCollection moc = searcher.Get())
                {

                }
            }
        }

        static void Main(string[] args)
        {
            Console.ReadKey();
            int times = 10000;
            for (int i = 0; i < times; i++)
            {

                Thread t = new Thread(o => dummyQuery("xxxxxxxxx", @"SELECT WorkingSetSize FROM Win32_Process WHERE name='W3WP.exe'"));
                //t.IsBackground = true;

                t.Start();

                System.Threading.Thread.Sleep(50);
            }
            Console.ReadKey();
            //GC.Collect();
            Console.ReadKey();
        }
    }
}

Is there a way to run WMI queries from threads safely?

This is extracted from a much more complicated wpf application that checks the status of many servers much like the dummyQuery method. That application leaks memory at a disturbingly fast rate related to WMI calls. This sample looks like it is not leaking memory (Jim Mischel had a better way of checking this). I will install a profiler and take another look at the original app.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:11:08+00:00Added an answer on June 9, 2026 at 2:11 am

    I Know that this is might be considered a dead thread but it was top of the search list when I was looking for a solution to the ManagementObjectSearcher memory leak issue that I was having.

    My application is a multithreaded application that was calling WMI on the main thread as part of the initialisation process. The application then spawned multiple thread non of which used WMI. However, the application kept leaking memory when run as a Windows service (it was OK if it was run as a standard executable).

    Putting [MTAThread] attribute on the Main entrypoint resolved the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have asked a question similar to this in the past but this is
I have a question similar to This one . But I want to implement
As a question similar to this question , I also have an application with
I have a question similar to this question but it is MonoTouch specific. When
A question similar to Locating bundles by identifier , but different problem: I have
There was a question similar to this one, but w/o the answer targeted at
there were a few question similar to mine on the stack but none that
I searched the question similar to my problem Similar problem . But my problem
I've asked a question similar to this but thought I would ask a more
I have a question similar to this but in the context of L2S. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.