locked
Start-Process starts to fail with exception "Not enough storage is available to process this command" after running one hour RRS feed

  • Question

  • Hi,

    I have a simple http server written in power shell, which simply receives a power shell script from the client, and creates a corresponding thread (via runspace) to deal with it - by calling "Start-Process" to launch powershell.exe to execute such the script. The code works fine at the beginning, but after running about one hour, "Start-Process" starts to raise exception: "Start-Process : This command cannot be executed due to the error: Not enough storage is available to process this command", I also roughly counted the number of successful "Start-Process" invocation - there is ~2300 invocations that succeeded, and then somehow it always fails with "no enough storage", but the host has quite a lot of disk/memory space left.

    Does anyone know why it is that and how this problem can be solved?

    Thanks and Best Regards!

    Saturday, July 8, 2017 5:25 AM

All replies

  • How many copies of PowerShell are you trying to run?  We can make no guesses as to what you script is doing.  It appears that your threads and processes are never terminating.

    PowerShell in not a very good way to try to create a web server.


    \_(ツ)_/

    Saturday, July 8, 2017 5:52 AM
  • Thank you very much for your reply! The code doesn't have the limitation on how many clients can connect to the server. Whenever there is a connection, the main thread of the server will create a new thread for it and then wait for the next connection - it also calls EndInvoke/Dispose for cleanup. And when the exception occurred, observed that previously launched processes had terminated. The main thread could create the new thread through run space, but the new thread just could not successfully call "start-process" anymore, that is very weird!

    Saturday, July 8, 2017 6:25 AM
  • It appears that you are not releasing and cleaning up your threads.  If you use a runspace factory you can declare a set of threads in a pool and then reuse tem as needed.  This will eliminate the constant creation of runspaces that do not get disposed and then cleaned up.

    Out side of that there is really nothing I can say. I recommend running under a memory diagnostic that records allocations and leaks.


    \_(ツ)_/

    Saturday, July 8, 2017 6:49 AM
    • Edited by JS2010 Saturday, July 8, 2017 11:23 PM
    Saturday, July 8, 2017 11:23 PM
  • Thanks a lot! I tried to increase MaxMemoryPerShellMB which defaults to 1024M in my server 2012 r2, but turned out it didn't work.
    Tuesday, July 11, 2017 9:01 AM
  • Thank you jrv! I tried to create a run space pool and set a throttle, but the issue was still there.
    Tuesday, July 11, 2017 9:03 AM