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:

Or in short:

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:

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:

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.

One comment

  1. Bjorn Houben says:

    Thanks, I’ll be using this to test if my SCOM monitor is working correctly.

Leave a Reply

Your email address will not be published. Required fields are marked *