Directly traverse the memory leakage after DirectorySearcher. FindAll ()

Source: Internet
Author: User

To write a small program these two days, you need to use the user who traverses the AD domain and regularly click it. According to the habit of simple writing, the following things will happen.
//////////////////////////////////////// //////////////////////////////////////// //
[Csharp]
DirectoryEntry ent = new DirectoryEntry (@ "LDAP: // domainName/OU = xxx, DC = xxx, DC = com", "aduserName", "adPassword "); // bind to the OU specified by AD
DirectorySearcher adSear = new DirectorySearcher (ent); // create a search
AdSear. Filter = "(& (objectCategory = person) (objectClass = user )(! (UserAccountControl: 1.2.840.113556.1.4.803: = 2 ))(! (Description = '')"; // This is a filtering condition and can be ignored.
AdSear. SearchScope = SearchScope. Subtree; // Force Search Method
Foreach (SearchResult resEnt in adSear. FindAll ();) // traverses all the searched results
{
// Do something not importent
} Www.2cto.com
AdSear. Dispose (); // destroy the search object
Ent. Dispose (); // destroy the root object


//////////////////////////////////////// //////////////////////////////////////// ////
The above is the loop body (it will run for about 20 s each time, my timer is 1 minute, and I have set the running judgment)

On the surface, there is no problem. After running for more than 10 minutes, we can see that the memory has increased from about 40 to about 50. The continued running will continue to increase, and there is no doubt that the memory leakage will occur. First, I suspected the Operation Problems in the middle of the loop body. I did not kill all the third-party classes, and finally found that the code would still leak. I declared two (ent, adSearch) I also destroyed two of them. How can this cause memory leakage?
I checked N more information and considered the GC. Collect () of the unmanaged memory. In addition, it didn't work... I even went to view dispose and finalize on msdn, but there was no result. Later, when I noticed the DirectorySearcher in MSDN. A remarks of FindAll Method: [Due to implementation restrictions, the SearchResultCollection class cannot release all of its unmanaged resources when it is garbage collected. to prevent a memory leak, you must call the Dispose method when the SearchResultCollection object is no longer needed. ], I can see it clearly.
In my code, the searchResultCollection is not explicitly declared, but adSear. FindAll () Suddenly exists, and there is no garbage collection for it in the code, so

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\\\
[Csharp]
....................................
SearchResultCollection resRCs = adSear. FindAll ();
Foreach (SearchResult resEnt in resRCs)
............................
ResRCs. Dispose ();


\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\\\\\\\\\
{PS: You can also directly use adSear. FindAll (). Dispose ();}
Then test again, no memory leakage
It took me a long time to think about such a thing. I commented, tested, and deleted countless codes in the middle ....
Here I found that many habits of writing are not necessarily good, and many professional programmers may also make mistakes. These errors can only be reflected in a specific environment. For example, if the search area is small and the number of executions is not large, you cannot feel the memory leakage.
Stay here and make a memo...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.