小编Ric*_*ard的帖子

使用PrincipalSearcher.FindAll()时内存泄漏

我也有使用插件和appdomains的长期服务,并且因使用目录服务而导致内存泄漏.请注意,我使用的是system.directoryservices.accountmanagement,但我理解它使用相同的底层ADSI API,因此容易出现相同的内存泄漏.

我查看了所有CLR内存计数器,并且内存没有泄漏,并且都是在强制GC或卸载appdomain时返回的.泄漏是私有字节,不断增长.我在这里搜索并看到了一些与使用ADSI API时内存泄漏相关的问题,但它们似乎表明只需遍历directorysearcher即可解决问题.但正如您在下面的代码中看到的那样,我在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); …
Run Code Online (Sandbox Code Playgroud)

.net c# directoryservices userprincipal principalcontext

4
推荐指数
2
解决办法
4494
查看次数