Today I ran into a situation where my powershell process memory utilization increased to ~2GB. I know why it happened. I ran a Powershell script from the PS window which queries event log from a few computers. The event log data will be high in volume generally and since PS keeps this run time data in memory, the process memory utilization increased to such a high value. In ideal world, the memory should get freed up at the end of script execution, but it didn’t happen.
I can close my powershell process to clear the memory but that is a good solution. Moreover, I did some work in that console and would like to retain/review that(like history). After searching for sometime to find a solution finally I landed on System.GC (Garbage collection) class in Dotnet. It has a method to perform garbage collection for current powershell process. I used the below command to release the memory used by current powershell process.
Do you have any other better method to do this? Feel free to comment.