UserPrincipal.GetGroups 泄漏

发布于 2024-11-06 15:56:38 字数 1284 浏览 0 评论 0原文

我已经找到了我的组枚举代码的泄漏情况。我已经编写了一个测试例程,我正在尝试处理所有内容,但它仍然泄漏。

有人看到我做错了什么吗?在我的测试中,我连续调用它 100 次,在我的小域上,内存占用量从 32MB 增加到超过 150MB。

    private void EnumGroups()
    {
        try
        {
            using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "domainname.com"))
            {

                using (PrincipalSearcher srch = new PrincipalSearcher())
                {
                    srch.QueryFilter = new UserPrincipal(context);

                    // do the search
                    using (PrincipalSearchResult<Principal> results = srch.FindAll())
                    {
                        // enumerate over results
                        foreach (UserPrincipal up in results)
                        {
                            using (PrincipalSearchResult<Principal> groups = up.GetGroups())
                            {
                                foreach (GroupPrincipal gp in groups)
                                    gp.Dispose();
                            }
                            up.Dispose();
                        }
                    }
                }
            }
        }
        catch (Exception exc)
        {
            Trace.WriteLine(exc);
        }
    }

I've tracked down a leak to my group enumeration code. I've written a test routine and I'm trying to dispose of everything but still it leaks.

Does anyone see what I'm doing wrong? In my test, I call it 100 times in a row and on my small domain, the memory footprint goes from 32MB to over 150MB.

    private void EnumGroups()
    {
        try
        {
            using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "domainname.com"))
            {

                using (PrincipalSearcher srch = new PrincipalSearcher())
                {
                    srch.QueryFilter = new UserPrincipal(context);

                    // do the search
                    using (PrincipalSearchResult<Principal> results = srch.FindAll())
                    {
                        // enumerate over results
                        foreach (UserPrincipal up in results)
                        {
                            using (PrincipalSearchResult<Principal> groups = up.GetGroups())
                            {
                                foreach (GroupPrincipal gp in groups)
                                    gp.Dispose();
                            }
                            up.Dispose();
                        }
                    }
                }
            }
        }
        catch (Exception exc)
        {
            Trace.WriteLine(exc);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉宸 2024-11-13 15:56:38

从另一个临时域执行 GetMembers:

dTaskTemporaryDomain = AppDomain.CreateDomain("BUHLO_POBEDIT_ZLO");
        var currentAssembly = Assembly.GetExecutingAssembly();
        AdsApiHelper = (AdsApiHelper)_adTaskTemporaryDomain.CreateInstanceAndUnwrap(currentAssembly.GetName().FullName, typeof(AdsApiHelper).ToString());
        var members = AdsApiHelper.GetMembers(GroupName);
 AppDomain.Unload(_adTaskTemporaryDomain)

...
[serializeble]
class AdsApiHelper {
  GetMembers(String GroupName){
   .....
   return groupPrincipal.GetMembers().Select(m=>m.SamAccountName);
  }

Execute GetMembers from another temp domain:

dTaskTemporaryDomain = AppDomain.CreateDomain("BUHLO_POBEDIT_ZLO");
        var currentAssembly = Assembly.GetExecutingAssembly();
        AdsApiHelper = (AdsApiHelper)_adTaskTemporaryDomain.CreateInstanceAndUnwrap(currentAssembly.GetName().FullName, typeof(AdsApiHelper).ToString());
        var members = AdsApiHelper.GetMembers(GroupName);
 AppDomain.Unload(_adTaskTemporaryDomain)

...
[serializeble]
class AdsApiHelper {
  GetMembers(String GroupName){
   .....
   return groupPrincipal.GetMembers().Select(m=>m.SamAccountName);
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文