Friday, January 20, 2012

PowerShell – Remote System call using invoke-command out of memory

Some memory errors might occur using using the invoke-command PowerShell cmdlet to run programs or scripts on remote hosts. By default a remote session is default limited to 150MB. For example when trying to run java application remote.

Invoke-Command -ComputerName MyServer-ScriptBlock {java}
Error occurred during initialization of VM
Could not reserve enough space for object heap

To solve this setting the MaxMemoryPerShellMB option for remote shell connections to a larger amount. This comment must be run on the remote system with administrator rights.


set-item wsman:localhost\Shell\MaxMemoryPerShellMB 2048


winrm set winrm/config/winrs @{MaxMemoryPerShellMB="2048"}

Now trying again to run invoke-command to the remote server yields better results.

Invoke-Command -ComputerName MyServer -ScriptBlock {java}
Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)

This can be affect by using PowerShell ISE Remote PowerShell Tab also.


Anonymous said...

Thankyou! I have been googling for hours on this problem. I had a Java VM (Oracle Universal Installer) mysteriously crashing on startup with no logging when I try to execute it remotely via Invoke-Command. Again, thanks for the post.

Patrick Nielsen said...

Glad it helped out. The default JVM memory size is greater then powershell remoting max size which causes these issues.

Preetam said...

Just wanted to say thanks for this.. exactly what I wanted to automate running a C# console app on a bunch of remote servers.

Just one thing.. This C# app is a multi threaded app doing the same thing on 2 threads.. but when I call the app (.exe) directly it runs only one thread.. but when I call a .bat file and have this batch file trigger the app then the 2 thread in the app run.. is there some kinda thread restrictions in powershell?

ps: the call to either the exe or bat is done via powershell Invoke-commad -scriptblock

Thanks again

Patrick Nielsen said...


I've not tried using a multi threaded application with PS remoting. But If you have threads for performance concerns i would look into PSExec
As for powershell I would have to say since you can natively use .net objects and background jobs I migrated a handful of scripts to powershell.
Background Jobs in Powershell

I'll let you know if i get a test in with multi threaded applications but my guess is its a safety limit.

Unknown said...

Thank you for this post.
The Invoke command works well, but when I try to login on the VM via powershell remoting and then running the following command, I get again the same error :
#Run java App in Powershell
java -jar .\ApacheJMeter.jar -n -t '.\MyTest.jmx'

"Error occurred during initialization of VM
Could not reserve enough space for object heap"

Can you kindly help me , Thank you

Tomo said...

Thanks Pat - that was the answer for me! Cheers

Rohan said...

Thank you very much for the blog!