私もプラグインとアプリドメインを使用して長時間実行されているサービスを持っており、ディレクトリサービスを使用しているためメモリリークが発生しています。私は system.directoryservices.accountmanagement を使用していることに注意してください。ただし、同じ基礎となる ADSI API を使用しているため、同じメモリ リークが発生しやすいことを理解しています。
私はすべての CLR メモリ カウンターを調べましたが、メモリはそこでリークされておらず、強制 GC またはアプリドメインをアンロードしたときにすべて返されます。リークは、継続的に増加するプライベート バイトにあります。ここで検索したところ、ADSI API を使用する際のメモリ リークに関連する問題がいくつか見られましたが、ディレクトリサーチャーを反復処理するだけで問題が解決するようです。しかし、以下のコードでわかるように、私は foreach ブロックでそれを行っていますが、それでもメモリ リークが発生しています。助言がありますか?これが私の方法です:
public override void JustGronkIT()
{
using (log4net.ThreadContext.Stacks["NDC"].Push(GetMyMethodName()))
{
Log.Info("Inside " + GetMyMethodName() + " Method.");
System.Configuration.AppSettingsReader reader = new System.Configuration.AppSettingsReader();
//PrincipalContext AD = null;
using (PrincipalContext AD = new PrincipalContext(ContextType.Domain, (string)reader.GetValue("Domain", typeof(string))))
{
UserPrincipal u = new UserPrincipal(AD);
u.Enabled = true;
//u.Surname = "ju*";
using (PrincipalSearcher ps = new PrincipalSearcher(u))
{
myADUsers = new ADDataSet();
myADUsers.ADUsers.MinimumCapacity = 60000;
myADUsers.ADUsers.CaseSensitive = false;
foreach (UserPrincipal result in ps.FindAll())
{
myADUsers.ADUsers.AddADUsersRow(result.SamAccountName, result.GivenName, result.MiddleName, result.Surname, result.EmailAddress, result.VoiceTelephoneNumber,
result.UserPrincipalName, result.DistinguishedName, result.Description);
}
ps.Dispose();
}
Log.Info("Number of users: " + myADUsers.ADUsers.Count);
AD.Dispose();
u.Dispose();
}//using AD
}//Using log4net
}//JustGronkIT
foreach ループに次の変更を加えたところ改善されましたが、プライベート バイトは依然として大きくなり、回収されることはありません。
foreach (UserPrincipal result in ps.FindAll())
{
using (result)
{
try
{
myADUsers.ADUsers.AddADUsersRow(result.SamAccountName, result.GivenName, result.MiddleName, result.Surname, result.EmailAddress, result.VoiceTelephoneNumber, result.UserPrincipalName, result.DistinguishedName, result.Description);
result.Dispose();
}
catch
{
result.Dispose();
}
}
}//foreach