A N
A N

Reputation: 113

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?

Upvotes: 11

Views: 10905

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200373

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

Upvotes: 14

Related Questions