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 5936905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:27:51+00:00 2026-05-22T15:27:51+00:00

I have a long running process that needs to do a lot of queries

  • 0

I have a long running process that needs to do a lot of queries on Active Directory quite often. For this purpose I have been using the System.DirectoryServices namespace, using the DirectorySearcher and DirectoryEntry classes. I have noticed a memory leak in the application.

It can be reproduced with this code:

while (true)
{
    using (var de = new DirectoryEntry("LDAP://hostname", "user", "pass"))
    {
        using (var mySearcher = new DirectorySearcher(de))
        {
            mySearcher.Filter = "(objectClass=domain)";
            using (SearchResultCollection src = mySearcher.FindAll())
            {
            }            
         }
    }
}

The documentation for these classes say that they will leak memory if Dispose() is not called. I have tried without dispose as well, it just leaks more memory in that case. I have tested this with both framework versions 2.0 and 4.0 Has anyone run into this before? Are there any workarounds?

Update: I tried running the code in another AppDomain, and it didn’t seem to help either.

  • 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-05-22T15:27:52+00:00Added an answer on May 22, 2026 at 3:27 pm

    As strange as it may be, it seems that the memory leak only occurs if you don’t do anything with the search results. Modifying the code in the question as follows does not leak any memory:

    using (var src = mySearcher.FindAll())
    {
       var enumerator = src.GetEnumerator();
       enumerator.MoveNext();
    }
    

    This seems to be caused by the internal searchObject field having lazy initialization , looking at SearchResultCollection with Reflector :

    internal UnsafeNativeMethods.IDirectorySearch SearchObject
    {
        get
        {
            if (this.searchObject == null)
            {
                this.searchObject = (UnsafeNativeMethods.IDirectorySearch) this.rootEntry.AdsObject;
            }
            return this.searchObject;
        }
    }
    

    The dispose will not close the unmanaged handle unless searchObject is initialized.

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (((this.handle != IntPtr.Zero) && (this.searchObject != null)) && disposing)
            {
                this.searchObject.CloseSearchHandle(this.handle);
                this.handle = IntPtr.Zero;
            }
        ..
       }
    }
    

    Calling MoveNext on the ResultsEnumerator calls the SearchObject on the collection thus making sure it is disposed properly as well.

    public bool MoveNext()
    {
      ..
      int firstRow = this.results.SearchObject.GetFirstRow(this.results.Handle);
      ..
    }
    

    The leak in my application was due to some other unmanaged buffer not being released properly and the test I made was misleading. The issue is resolved now.

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

Sidebar

Related Questions

I have a python web application that needs to launch a long running process.
I have a long running, compute and disk intensive task that needs to run
I have a long running process that integrates over a collection of incoming Entities
I have a long running timer task. Its a batch process actually that will
I have an application for PDAs with a long running process, and I'm getting
I have a situation where user requests to do a long running process. I
I have a long-running cleanup operation that I need to perform in onDestroy() of
I occasionally have some long running AJAX requests in my Wicket application. When this
I have a long-running SP (it can run for up to several minutes) that
I'm currently working on a project that needs to have a process that assigns

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.