How to increase memory usage with powershell

I am trying to do some testing that requires resources to be under pressure. I was able to learn how to maximize CPU usage with PowerShell.

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

Can PowerShell be used to maximize memory usage?

+4
source share
1 answer

Try the following:

 $mem_stress = "a" * 200MB 

Edit: If ku cannot allocate all the memory in one fragment, try allocating several blocks in the array:

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

GC does not seem to interfere with this (results of a quick test on a virtual machine with 1 GB of 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 
+6
source

Source: https://habr.com/ru/post/1436708/


All Articles