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...