0

How can I measure memory usage of a command in powershell. I'm thinking something like "Maximum resident memory" GNU time returns with -v flag. Measure-Command only measures time.

I also found this in my searches https://communary.net/2014/10/28/measure-scriptblock/ but its method for measuring memory use is clearly broken. It ouputs 0 kB for programs that definitely use a few megabytes.

My experience of shell scripting is mostly with bash, unfortunately. I found a few solutions that output the momentary memory use of a process using powershell. I tried using Start-Job, but running my same command inside Start-Job seems to kill it right away (It's an exe in the local directory). And anyway, I'm interested in total memory use by the process, not the momentary use at a point in time.

1 Answer 1

1

Are you looking for processes memory usage -- or memory usage of a function in your script?

Check out Get-Process It returns System.Diagnostics.Process which have a bunch of properties related to memory usage. The link you had is using the same class.

He's also using [gc] the garbage collector, to try to force memory to be collected early. If you're not testing it's own process, you can skip that. Spawning a child to measure might be more reliable.

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to run an executable in the local directory and get-process on that?
I figured out my mistake. Powershell, unlike unix shells, seems to run start-job from home directory, so I added -Init ([ScriptBlock]::Create("Set-Location '$pwd'")) to it.
Powershell added the parameter -WorkingDirectory for that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.