Score:0

PowerShell ForEach-Object Parallel RAM Memory Keeps Growing

in flag

Using PS 7.3.4 The code works fine without errors.

Issue - the RAM consumed by the script keeps growing. How do I release the RAM? How do I see what is consuming the RAM? Any suggestions will be greatly apprciated.

Memory results after processing batches of 10K input

End of 1st group of 10K memory results.

For loop memory used BEFORE collection: 1,208,928,432

For loop memory used AFTER full collection: 512,021,912

End of 2nd group of 10K memory results.

For loop memory used BEFORE collection: 1,160,822,952

For loop memory used AFTER full collection: 929,192,840

End of 3rd group of 10K memory results.

For loop memory used BEFORE collection: 2,168,048,568

For loop memory used AFTER full collection: 1,355,942,720

. . .

End of 8th group of 10K memory results.

For loop memory used BEFORE collection: 12,562,155,960

For loop memory used AFTER full collection: 11,765,923,128

End of 9th group of 10K memory results.

For loop memory used BEFORE collection: 13,170,803,320

For loop memory used AFTER full collection: 12,166,518,384

[int]$i3 = 1

Loop through all of the arrayLists in $allArrays. Each arrayList inside $allArrays contains 10K user ids.

Do {

$allArrays[$i3] | ForEach-Object -Parallel {

# Contains code to call Microsoft graph API, get some info and write to file.

# At end of script, write all results to arrayList.
# Write all arrayLists to a file.
# Empty all arrayLists using arrayList.Clear()

# # Code to track variables that keep growing in size INSIDE foreach parallel.  Unfortunately the variables information returned does not show any variables exceeding a length of 3000.  Even after processing 200K input lines.
$varSize = get-variable -Name *
for ($i=0; $i -lt $varSize.Count; $i++)
    {
        if($varSize[$i].Value -and ($varSize[$i].Value.ToString().Length -gt 3000)){Write-host -ForegroundColor gray "`n`rDisplaying all variables with a length greater than 50"; Write-host -ForegroundColor gray "Name: $($($varSize[$i]).Name) `tLength: $($varSize[$i].Value.ToString().Length)"}
    }

}
# Function called to renew access token.
renewAccessToken

$memBefore = '{0:N0}' -f $([System.GC]::GetTotalMemory($false))
write-host -ForegroundColor Gray "`n`rFor loop memory used BEFORE collection: $memBefore - $(getTimestamp)"
[System.GC]::Collect()
[GC]::Collect()
[GC]::WaitForPendingFinalizers()
$memAfter = '{0:N0}' -f $([System.GC]::GetTotalMemory($true))
write-host -ForegroundColor Gray "For loop memory used AFTER full collection: $memAfter - $(getTimestamp)"

# Code to track variables that keep growing in size OUTSIDE foreach parallel.  Unfortunately the variables information returned does not show any variables exceeding a length of 100.  Even after processing 200K input lines.
$varSize = get-variable -Name *
for ($i=0; $i -lt $varSize.Count; $i++)
    {
        if($varSize[$i].Value -and ($varSize[$i].Value.ToString().Length -gt 50)){Write-host -ForegroundColor gray "`n`rDisplaying all variables with a length greater than 50"; Write-host -ForegroundColor gray "Name: $($($varSize[$i]).Name) `tLength: $($varSize[$i].Value.ToString().Length)"}
    }
        
$i3++

} while(($allArrays.Count + 1) -ne $i3) 
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.