Quantcast
Viewing all articles
Browse latest Browse all 120

A PowerShell oneliner that exponentially consumes all memory

Why would you want to write a script that intentionally consumes memory exponentially?
I find that for demo and test purposes this is very useful, especially when it comes to demonstrate Dynamic Memory in Hyper-V.

So I wrote the following oneliner:

do { $a += Get-Process } until ( !$a )

Or in short:

do{$a+=gps}until(!$a)

Now these oneliners will exponentially consume memory, but as you will notice it is very (!) slow…

You could use Get-ChildItem instead of Get-Process but it will give you errors for locations where it doesn’t have access to.
You could do something with the -ErrorAction parameter, but for the purpose of a demo that’s not required:

do{$a+=gci c: -rec}until(!$a)

But as I noticed some time ago in an ITCamp which I was attending there is a far more elegant and more effective way to accomplish this task!
Jaap Brasser, also a member of DuPSUG, was attending the ITCamp as well and he came up with the following oneliner:

1..50|%{$x=1}{[array]$x+=$x}

This works great! Just try it and see your memory being consumed exponentially and much faster compared to the options I’ve previously mentioned in this post.


Viewing all articles
Browse latest Browse all 120

Trending Articles