≡ Menu

Tip: Clear the memory used by a Powershell Process

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.

[System.GC]::Collect()

Do you have any other better method to do this? Feel free to comment.

Comments on this entry are closed.

  • Amir Ahmadi November 6, 2018, 10:54 am

    Thanks A lot for your guide…

  • doesntWork March 20, 2022, 5:36 am

    It doesn’t work in my case. After finished script PowerShell ISE still holds 4 GB of ram!

    • jimbob July 6, 2022, 7:05 pm

      you need to empty any variables or pscustomobjects out that are no longer in use before running the GC command, otherwise they are still going to consume RAM