Friday, August 31, 2012

Memory Stats in the Title Bar (Part II)


Two different attempts to update memory stats in the title bar in the console background. The 'start-job' script won't work because it updates the new Powershell instance the job is running in. The 'runspace' script works because it updates the existing console.

# with Start-Job
# Doesn't Work because it updates a hidden Powershell Job window
start-job -InitializationScript {

function Global:Set-title {
$PSID=([System.Diagnostics.Process]::GetCurrentProcess()).Id
$MemStats=ps -id $PSID | Select `
@{Name='ThreadCount';Expression={($_.Threads).count}}, `
@{Name='WorkSetMB';Expression={[int](($_.WorkingSet64)/1MB)}}, `
@{Name='VirMemMB';Expression={[int](($_.VirtualMemorySize64)/1MB)}}, `
@{Name='PriMemMB';Expression={[int](($_.PrivateMemorySize64)/1MB)}}, `
@{Name='PagedMemMB';Expression={[int](($_.PagedMemorySize64)/1MB)}}, `
@{Name='NonPagedMemKB';Expression={[int](($_.NonPagedSystemMemorySize64)/1KB)}}

$Title=write "Last_Title_Stats: Time: $([datetime]::now) Version: $((get-host).Version.Major) SessionHours: $([int]([datetime]::now - (ps -id $PSID).Starttime).totalhours) Memory: $($Memstats) GC_MB: $([int]([GC]::gettotalmemory(1)/1MB))"
[console]::set_title($Title)
}

} `

-scriptblock {while(1) {set-title;sleep -s 5}}


# With Runspace
# Works because the new runspace refers to the existing Powershell window
$set_title=

{

function Global:Set-title {
$PSID=([System.Diagnostics.Process]::GetCurrentProcess()).Id
$MemStats=ps -id $PSID | Select `
@{Name='ThreadCount';Expression={($_.Threads).count}}, `
@{Name='WorkSetMB';Expression={[int](($_.WorkingSet64)/1MB)}}, `
@{Name='VirMemMB';Expression={[int](($_.VirtualMemorySize64)/1MB)}}, `
@{Name='PriMemMB';Expression={[int](($_.PrivateMemorySize64)/1MB)}}, `
@{Name='PagedMemMB';Expression={[int](($_.PagedMemorySize64)/1MB)}}, `
@{Name='NonPagedMemKB';Expression={[int](($_.NonPagedSystemMemorySize64)/1KB)}}

$Title=write "Last_Title_Stats: Time: $([datetime]::now) Version: $((get-host).Version.Major) SessionHours: $([int]([datetime]::now - (ps -id $psid).Starttime).totalhours) Memory: $($Memstats) GC_MB: $([int]([GC]::gettotalmemory(1)/1MB))"
[console]::set_title($Title)
}

while(1) {set-title;sleep -s 5}

}

$ST_Runspace = [PowerShell]::Create().AddScript($set_title)
$Begin_Set_Title = $ST_Runspace.BeginInvoke()

# Commands to query,stop,dispose the titlebar update:
# $ST_Runspace.runspace
# $Begin_Set_Title
# $Stop_Set_Title = $ST_Runspace.Stop()
# $Dispose_Set_Title = $ST_Runspace.Dispose()

Monday, August 27, 2012

Memory Stats in the Title Bar (Part I)

#This function puts memory stats for the first instance of Powershell in your title bar.
function Global:Set-title {
$MemStats=(ps powershell)[0] | Select `
@{Name='ThreadCount';Expression={($_.Threads).count}}, `
@{Name='WorkSetMB';Expression={[int](($_.WorkingSet64)/1MB)}}, `
@{Name='VirMemMB';Expression={[int](($_.VirtualMemorySize64)/1MB)}}, `
@{Name='PriMemMB';Expression={[int](($_.PrivateMemorySize64)/1MB)}}, `
@{Name='PagedMemMB';Expression={[int](($_.PagedMemorySize64)/1MB)}}, `
@{Name='NonPagedMemKB';Expression={[int](($_.NonPagedSystemMemorySize64)/1KB)}}
# one line below...
$Title=write "Last_Title_Stats: Time: $([datetime]::now) Version: $((get-host).Version.Major) SessionHours: $([int]([datetime]::now - (ps powershell)[0].Starttime).totalhours) Memory: $($Memstats) GC_MB: $([int]([GC]::gettotalmemory(1)/1MB))"
[console]::set_title($Title)
}

# To see...click on this image to enlarge.:


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:
[System.gc]::collect()
# 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
[System.gc]::collect()
#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}}, `
@{Name='PriMemMB';Expression={($_.PrivateMemorySize64)/1MB}}


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

Sunday, August 5, 2012

Some notes on querying variables in Powershell 3.0


PS C:\ps1> ls variable:/*


Name                           Value
----                           -----
null
false                          False
true                           True
MaximumErrorCount              256
MaximumVariableCount           4096
MaximumFunctionCount           4096
MaximumAliasCount              4096
MaximumDriveCount              4096
Error                          {Cannot find drive. A drive with the name 'variablwe' does not exist., System.Management.Automation.P...
PSDefaultParameterValues       {}
$                              variablwe:/*
^                              ls
StackTrace                        at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object inpu...
ErrorView                      NormalView
LASTEXITCODE                   0
PSEmailServer
....


PS C:\Windows\system32> ((ls variable:/) | gm *).typename | get-unique
System.Management.Automation.PSVariable
System.Management.Automation.QuestionMarkVariable
System.Management.Automation.LocalVariable
System.Management.Automation.SessionStateCapacityVariable
System.Management.Automation.NullVariable