Monday, August 20, 2012

Clearing up variables and memory in Powershell 3.0

Some GC lines can help you check and clear up memory  in Powershell 3.0:

# Find out how much memory is being consumed by your Sesssion:
[System.gc]::gettotalmemory("forcefullcollection") /1MB
# Force a collection of memory by the garbage collector:
# Dump all variables not locked by the system:
foreach ($i in (ls variable:/*)) {rv -ea 0 -verbose $i.Name}
#Check memory usage again and force another collection:
[System.gc]::gettotalmemory("forcefullcollection") /1MB
#Check Memory once more:
[System.gc]::gettotalmemory("forcefullcollection") /1MB

#You can examine the difference before and after with:
ps powershell* | Select *memory*

or with something more complicated:

ps powershell* | Select *memory* | ft -auto `
@{Name='VirMemMB';Expression={($_.VirtualMemorySize64)/1MB}}, `

    VirMemMB     PriMemMB
    --------     --------
1548.3671875 432.43359375
 603.9765625   58.5234375

No comments: