C# - Outofmemoryexception when searching for thousands of Active Directory groups

Asked

Viewed 69 times

1

I’m searching thousands of groups with hundreds of users each of Active Directory, but it’s consuming a lot of memory. It starts with about 300 MB and when it reaches about 1800 MB, seen in the task manager, an Outofmemoryexception is released. The exception is made when this part of the code is being executed.

foreach (string objectGUID in listGroups)
{
    GroupDTO objGroup = new GroupDTO();

    objGroup = ADHelper.GetGroupByGUID(objectGUID, domainInfo.GeneratePathOfDomainObj(domainInfo.RootDN), domainInfo.UserName, domainInfo.Password, domainInfo.AuthType, new string[] { "distinguishedname", "name", "objectGUID", "samaccountname", "mail", "description", "whenchanged", "grouptype", "primaryGroupToken" });

    List listUsersGroup = new List();

    //Uma lista com todos os ID (ObjectGUID) do grupo atual
    listUsersGroup = ADHelper.GetUsersFromGroup(objectGUID, domainInfo.GeneratePathOfDomainObj(domainInfo.RootDN), domainInfo.UserName, domainInfo.Password, domainInfo.AuthType, new string[] { "objectguid" }, domainInfo.PropertiesGroupsAndUsers, domainInfo.RootDN);

    Dictionary dicGroup = new Dictionary();
    string jSonGroup = JsonConvert.SerializeObject(objGroup);
    dicGroup.Add("group", jSonGroup);
    dicGroup.Add("lstUser", listUsersGroup); 

    arrayListGroups.Add(dicGroup);
}
  • You need to see if there are other complicated things besides that and only then it pops. But it can be right there. There is no miracle. Are you using 32bits? The limit is more or less the same. Will need all this together?

  • Segment your implementation, via logical unit, blocks or pages. Work with subsets, for example group by group. Do not load everything into memory at the same time.

  • This part of the code is from the Importgroups(listGroups) method. I have already tried to divide the listGroups into four more or less equal parts and perform Importgroups(listGroups) for each of these parts. I tried even splitting 10 parts and running, one at a time, the Importgroups(), but always the memory goes up absurdly until reaching some 1800 MB ~ 2000 MB in the Task Manager and give Outofmemoryexception (I am an intern, so forgive me if I’m doing nonsense).

  • It was resolved. The problem was within the Getusersfromgroup() method that instantiated a class that received AD search results without a Dispose() accumulating resources indefinitely in memory. Thanks for the answers :)

1 answer

0

I’ve had problems of Outofmemoryexception, I’ve resolved putting the objects out of the loop like this:

GroupDTO objGroup;
List listUsersGroup;
Dictionary dicGroup;
foreach (string objectGUID in listGroups)
{
    objGroup = new GroupDTO();

    objGroup = ADHelper.GetGroupByGUID(objectGUID, domainInfo.GeneratePathOfDomainObj(domainInfo.RootDN), domainInfo.UserName, domainInfo.Password, domainInfo.AuthType, new string[] { "distinguishedname", "name", "objectGUID", "samaccountname", "mail", "description", "whenchanged", "grouptype", "primaryGroupToken" });

    listUsersGroup = new List();

    //Uma lista com todos os ID (ObjectGUID) do grupo atual
    listUsersGroup = ADHelper.GetUsersFromGroup(objectGUID, domainInfo.GeneratePathOfDomainObj(domainInfo.RootDN), domainInfo.UserName, domainInfo.Password, domainInfo.AuthType, new string[] { "objectguid" }, domainInfo.PropertiesGroupsAndUsers, domainInfo.RootDN);

    dicGroup = new Dictionary();
    string jSonGroup = JsonConvert.SerializeObject(objGroup);
    dicGroup.Add("group", jSonGroup);
    dicGroup.Add("lstUser", listUsersGroup); 

    arrayListGroups.Add(dicGroup);
}
  • I saw this answer from the bigown: https://answall.com/questions/225192/ficar-criando-vari%C3%A1veis-local-o-tempo-todo-gera-custo-a-mais-para-o-software/225196#225196 but I don’t know if it applies to objects

Browser other questions tagged

You are not signed in. Login or sign up in order to post.