Reputation: 85
Can somebody explain to me why even after quitting excel via $excel.quit() and putting all the related variable to $null via $excel = $workbook = $worksheet = $null And collecting garbage via [gc]::collect() I’m unable to get the excel process to stop unless I use [System.GC]::GetTotalMemory($true) multiple time and only if I don’t use any of these to not get output: [void][System.GC]::GetTotalMemory($true), [System.GC]::GetTotalMemory($true)|out-null, $null = [System.GC]::GetTotalMemory($true)?
This happened no matter if I use [System.GC]::WaitForPendingFinalizers()
Interestingly if an error occurs and get thrown in the console the process can be stop even only with [gc]::collect(), but twice or trice
If the script get executed via cmd command powershell.exe -file “path to file” the process stop even after a single call to [gc]::collect().
I don’t send the script as it is very long but if it is necessary i can let you have the files via some hosting services.
I referred to this link: Can't get all excel processes to stop when closing through Powershell
This one: Under what circumstances, we need to call GC.Collect twice
And this one: https://www.jhouseconsulting.com/jhouseconsulting/2017/09/25/addressing-the-powershell-garbage-collection-bug-1825
Thank you in advance
Franco
Upvotes: 0
Views: 41