I know the title is fascinating but this little nugget of knowledge came in extremely useful. I was writing an web application that talked to Active Directory (using VB .net) for some of it’s info and found that there was a gigantic memory leak occurring.
When I set up my search filter and gathered a search result using the .findone() method problems ensued.
A lot of programmers seem to rely on the garbage collector and this just isn’t cool. What was happening was that the garbage collector was not properly disposing of the the directory searcher I had initialized. The solution:
Explicitly dispose of the Directory Searcher!!!!
These examples are using VB code:
example #1:
Dim ds as new DirectorySearcher()
’some code in here
‘cleaning up here
ds.Dispose()
or example #2:
Using ds as new DirectorySearcher()
’some code here
End Using
Following either of these examples will help you avoid memory leaks when using a Directory Searcher in your .net code. I think it is a good idea to manually dispose of your objects whenever possible. So if I say it you should definitely do it
Let me know if you found this useful!
Update:
I forgot to mention that if you are doing any sort of programming in .net against Active Directory you need to go read “The .NET Developer’s Guide to Directory Services Programming” The Authors seriously know their stuff!