Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I maximize memory usage using powershell

I am trying do some testing that requires resources to be under pressure. I was able to find out how maximize CPU Usage using PowerShell.

start-job -ScriptBlock{
$result = 1; 
foreach ($number in 1..2147483647) 
    {
        $result = $result * $number
    }
}

Is there a way I can use PowerShell to do the same for maximizing memory usage?

like image 787
A N Avatar asked Sep 27 '12 22:09

A N


Video Answer


1 Answers

Try this:

$mem_stress = "a" * 200MB

Edit: If cou can't allocate all memory in a single chunk, try allocating multiple chunks in an array:

$mem_stress = @()
for ($i = 0; $i -lt ###; $i++) { $mem_stress += ("a" * 200MB) }

The GC doesn't seem to interfere with this (results from a quick test in a VM with 1 GB RAM assigned):

PS C:\> $a = @()
PS C:\> $a += ("a" * 200MB)
PS C:\> $a += ("a" * 200MB)
PS C:\> $a += ("a" * 200MB)
PS C:\> $a += ("a" * 200MB)
The '*' operator failed: Exception of type 'System.OutOfMemoryException' was thrown..
At line:1 char:13
+ $a += ("a" * <<<<  200MB)
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : OperatorFailed
like image 184
Ansgar Wiechers Avatar answered Sep 28 '22 05:09

Ansgar Wiechers