Memory management

Hello @adam !
I found a post regarding objects that implement IDisposable interface and it was very useful … (i have a custom MySQL and forgot to dispose the object at the end of the function … and since i’ve implemented it… well the memory consumed is much lower )

This bring a question and maybe a reflexion on how to keep my PS memory clean!
Should i remove-variable at the end of each endpoint ?? and maybe do a [System.GC]::Collect() at the end of each endpoint ??
Any idea ?? to keep the memory relativly low ?

Something else i implemented, but did not get much result, for example, is remove closed remote runspaces.

(at the moment we are still using UD, but this might be applicable to PSU )

Remove-Variable - This isn’t necessary because we call ResetRunspaceState after each execution. This effectively is remove all variables automatically: Runspace.ResetRunspaceState Method (System.Management.Automation.Runspaces) | Microsoft Docs

GC.Collect - This could be helpful. Most of the time it’s considered bad practice and can reduce performance since it calls the garbage collector more often than the system deems necessary. That said, it seems like PS can cause some weird reference issues so I’ve heard of this helping.

IDisposable - This is important to implement. It ensures that the garbage collector knows it’s ok to dispose of unused resources. IDisposable objects will eventually be cleaned up but it may take some time if not manually disposed.

1 Like