Question : OutOfMemoryException when reading users from AD

Hi experts

I wrote method which reads users from AD and put their data into the SortedList. However, after it reads moreless 3000 users exception OutOfMemory is thrown. Does anybody know how to resolve this problem?

Method I wrote to read users is shown below

Thanks for all help
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
Class UserData
    Public nGuid As Int64 = 0
    Public csLogin As String = ""
    Public csCommonName As String = ""
    Public cUserGroups As Collections.Generic.List(Of String)
  End Class
  Dim m_cUsersColl As Collections.Generic.SortedList(Of String, UserData)


Sub ReadADUsers()
    If Me.dc_address.Text = "" Then
      MsgBox("Insert server address")
      Exit Sub
    End If

    If Me.dc_path.Text = "" Then
      MsgBox("Insert LDAP path")
      Exit Sub
    End If

    If Me.dc_flt.Text = "" Then
      MsgBox("Insert filter")
      Exit Sub
    End If

m_cUsersColl = New Collections.Generic.SortedList(Of String, UserData)
    m_cUsersColl.Clear()

    Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://" & dc_address.Text & "/" & dc_path.Text)

    Dim oSearcher As DirectorySearcher = New DirectorySearcher(oRoot)
    Dim oResults As SearchResultCollection
    Dim oResult As SearchResult

    oSearcher.PageSize = 5000

    Try

      Dim nUserCount = 0

      oSearcher.Filter = dc_flt.Text

      oSearcher.PropertiesToLoad.Add("uid")
      oSearcher.PropertiesToLoad.Add("cn")
      oSearcher.PropertiesToLoad.Add("sAMAccountName")
      oSearcher.PropertiesToLoad.Add("memberOf")

      oResults = oSearcher.FindAll()
      For Each oResult In oResults

        If Not oResult.GetDirectoryEntry().Properties("cn").Value = "" Then

          nUserCount = nUserCount + 1

          Dim cUserData As UserData = New UserData
          cUserData.nGuid = oResult.GetDirectoryEntry().Properties("uid").Value
          cUserData.csCommonName = oResult.GetDirectoryEntry().Properties("cn").Value
          cUserData.csLogin = oResult.GetDirectoryEntry().Properties("sAMAccountName").Value
          cUserData.cUserGroups = New Collections.Generic.List(Of String)

          Dim csGroupString As String = ""
          Dim nMemberOfCnt = 0
          For nMemberOfCnt = 0 To oResult.GetDirectoryEntry().Properties("memberOf").Count - 1

            Dim csGroupName As String = CType(oResult.GetDirectoryEntry().Properties("memberOf")(nMemberOfCnt), String)
            Dim nStartIdx = csGroupName.IndexOf("=", 1)
            Dim nEndIdx = csGroupName.IndexOf(",", 1)

            csGroupString = ""
            If nStartIdx > 0 And nEndIdx > 0 And nEndIdx > nStartIdx Then
              csGroupString = csGroupName.Substring(nStartIdx + 1, nEndIdx - nStartIdx - 1)
            End If

            If csGroupString <> "" Then
              cUserData.cUserGroups.Add(csGroupString)
            End If
          Next

          m_cUsersColl.Add(cUserData.csLogin, cUserData)
        End If
      Next

      MsgBox("Search finished. Found " & nUserCount & " users.")
    Catch ex As Exception
      MsgBox("Search interrupted with error: " & ex.Message)
    End Try
  End Sub

Answer : OutOfMemoryException when reading users from AD

Use 0.jpg to get a larger image, like this:

http://img.youtube.com/vi/J4YjmwCs6H0/0.jpg
Random Solutions  
 
programming4us programming4us