1

I have a memory problem issue I need help with for powershell. For the following code:

function Get-PathPermissions {
        param ( [Parameter(Mandatory=$true)] [System.String]${Path} )
process {
    $groups=$null
    $groups=@{}
    $containers = Get-ChildItem -path $Path -recurse | ? {$_.psIscontainer -eq $true}
    if ($containers -eq $null) {break}
    foreach ($container in $containers) {
        Write-Output "Container: " $container
        $accts = (Get-ACL $container.fullname).Access
        foreach($acct in $accts) {
            $acctsQADObject = Get-QADObject -Identity ([string]$acct.IdentityReference) 
            if ($acctsQADObject.ObjectClass -contains 'group') {
                $Name = $acctsQADObject.Name
                $DN = $acctsQADObject.DN

                if (!$groups.ContainsKey($Name)) {
                    Write-Output "Adding $($Name) and $($DN)"
                    $groups.add($($Name),$($DN))
                }

            }
        }
    }
    foreach ($key in $groups.GetEnumerator() | Sort-Object Name -descending) {
        dumpGroup($key.value)
    }
}                
}

For every $accts loop, my memory increases. I start at 1.41 GB for example, and it just keeps climbing 1.42, 1.43 ... 3.00GB+. It is not the adding to the groups hash as there are only two groups seen for testing. Do I need to set $accts = $null every time? I would think each loop would create $accts over, destroying he previous one.

zdan
  • 28,667
  • 7
  • 60
  • 71
archcutbank
  • 419
  • 1
  • 6
  • 17

1 Answers1

0

you could try to call the garbage collection after processing each file.

Thomas Lee
  • 1,158
  • 6
  • 13
  • Wonderful. How do you do that? – archcutbank Aug 16 '12 at 13:54
  • I found this and tried it in the $accts loop: if (($counter % 200) -eq 0) { Write-Output "Running garbage collection"; [System.GC]::Collect() } but memory is still climbing even though I see it announce that it is Running the garbage collection. – archcutbank Aug 16 '12 at 14:03
  • 1
    I abandoned trying to use Quest or 2008 R2 ActiveDirectory cmdlets and instead used adsisearcher which was very fast and very nice with memory in comparison. – archcutbank Aug 17 '12 at 19:37
  • @user372429 - [gc]::collect() – Thomas Lee Aug 25 '12 at 13:26