Last active
October 30, 2016 10:30
-
-
Save daxian-dbw/411ccb9f71a4273db79add3b238b1f66 to your computer and use it in GitHub Desktop.
A powershell function to consume as much memory as possible
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
## A helper to check the memory limit of a PS remote session | |
function Consume-Memory | |
{ | |
## Get the total physical memory size | |
$totalPhysicalMem = get-ciminstance -class "cim_physicalmemory" | Measure-Object -Sum -Property Capacity | % Sum | |
Write-Host "Total Free Physical Memory: $($totalPhysicalMem / 1gb) GB" -ForegroundColor Green | |
## Create a two-dimensional array (20, N) | |
$array = @(,@(1)) * 20 | |
foreach ($i in 0..19) | |
{ | |
## Concatenate arrays in exponential way, to comsume memory as much as possible. | |
## When we hit an OutOfMemoryException, it means CLR can no longer allocate a large array | |
## for twice the size of `$array[$i]`. In this case, there still could be a lot memory | |
## left, so we go another round to consume rest of the memory. 20 rounds should be enough | |
## to run out almost all memory. | |
foreach ($p in 1..30) | |
{ | |
try { | |
[array]$array[$i] += $array[$i] | |
} catch [System.OutOfMemoryException] { | |
$s = gps -Id $pid | |
Write-Host "OutOfMemoryException caught. `$array[$i].Count: $($array[$i].Count); WorkingSet: $($s.WorkingSet64/1gb) GB" -ForegroundColor Yellow | |
break | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment