This is what it looks like:
Created
June 23, 2025 13:52
-
-
Save nohwnd/43c5c5ace6651af3356025e5f712f03b to your computer and use it in GitHub Desktop.
Show memory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Add-Type -TypeDefinition @" | |
| using System; | |
| public static class MemoryObserver | |
| { | |
| private static System.Timers.Timer _timer; | |
| private static int _limit; | |
| private static System.Management.Automation.Host.PSHostRawUserInterface _rawUi; | |
| public static void Setup(System.Management.Automation.Host.PSHostRawUserInterface rawUi, int limit) | |
| { | |
| _limit = limit; | |
| _rawUi = rawUi; | |
| _timer = new System.Timers.Timer | |
| { | |
| Enabled = true, | |
| AutoReset = true, | |
| Interval = 500, | |
| }; | |
| _timer.Elapsed += OnElapsed; | |
| } | |
| private static void OnElapsed(object sender, System.Timers.ElapsedEventArgs e) | |
| { | |
| var memory = GC.GetTotalMemory(forceFullCollection: false) / (1024 * 1024); | |
| if (memory < _limit) | |
| { | |
| _rawUi.WindowTitle = $"😊 {memory} MB"; | |
| } | |
| else | |
| { | |
| _rawUi.WindowTitle = $"☠️☠️☠️ {memory} MB"; | |
| } | |
| } | |
| } | |
| "@ | |
| [MemoryObserver]::Setup($host.UI.RawUI, <# memory limit in MB #> 300) | |
| ## Remove code below for real examples | |
| Write-Host "Idling look in the title." | |
| Start-Sleep -seconds 5 | |
| Write-Host "Starting to eat memory" | |
| $eatMem = 1..1000 | % { Get-Process } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
