【问题标题】:Memory leaks when using DirectoryServices.AccountManagement使用 DirectoryServices.AccountManagement 时出现内存泄漏
【发布时间】:2013-11-09 19:56:16
【问题描述】:

我们将 Web 系统移至 Windows 身份验证。将其部署到生产环境后,我们面临内存泄漏。我们使用 poolmon.exe util 定义它是分页池内存泄漏(标记 Toke)。 在最近的修改中,我们只添加了以下两种方法:

using System.DirectoryServices.AccountManagement;


private bool IsLoginValid(string login, string password)
{
    bool isValid = false;
    using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domainName))
    {
        isValid = pc.ValidateCredentials(login, password);
    }
    return isValid;
}

private bool isMemberOf(string login, string group)
{
    bool result = false;           
    using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domainName))
    {
        using (UserPrincipal user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, login))
        {
            if (user != null)
            {
                result = user.IsMemberOf(pc, IdentityType.Name, group);
            }
        }
    }
    return result;
}

请帮助确定泄漏的确切点,并在可能的情况下提供解决方法。谢谢。

【问题讨论】:

    标签: c# asp.net memory-leaks directoryservices


    【解决方案1】:

    PrincipalContext 和/或UserPrincipal 的实现中可能存在错误,导致无法自动处置实例。我有seen this previously。您可以通过将using 替换为try-finally 来轻松确认/修复此问题,如下所示。

    PrincipalContext pc = new PrincipalContext(ContextType.Domain, domainName);
    try
    {
         isValid = pc.ValidateCredentials(login, password);
    }
    finally
    {
         pc.Dispose();
    }
    

    【讨论】:

    • new 关键字与显式接口实现一起导致 try/finally 调用与 using 调用不同。哎哟。要检查是否是这种情况,只需在 finally 块中添加显式转换,看看行为是否不同:((IDisposable)pc).Dispose();
    • 谢谢,但是我发现UserPrincipal.FindByIdentity()这个方法有内存泄漏。
    【解决方案2】:

    方法UserPrincipal.FindByIdentity()有内存泄漏。

    【讨论】:

      猜你喜欢
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多