When using pwsh script as a service, what methods can be used to reduce the memory overhead caused by the pwsh instance?

When using pwsh script as a service, what methods can be used to reduce the memory overhead caused by the pwsh instance?

aiac,
Welcome to the forum. :wave:t3:

When the memory consumption of a pwsh script/session running as a service causes issues you may upgrade the size of the memory. Otherwise it may be better not to use a pwsh script. :man_shrugging:t3:

In order to potentially assist, we’d have to know more about the script/session you’re running and also what you’re trying to accomplish, and what you define as ‘memory overhead’ Are you referring to just memory it takes to run the executable, or are you also referring to all the other memory usage associated with a PS session, which will include things like data stored in variables. As Olaf mentioned, increasing memory obviously would be a stop gap to fix an issue, but it’s very hard for us to help with general question of ‘what methods’ without knowing more about what you’re doing.

For example, is this PS Script collecting a lot of data and doing things with it? Everything that is stored in a variable is going to use memory. There might be ways of splitting up tasks so that not all of that memory is used. For example you might have a collector script that collects different sets of data and writes them out to disk, then maybe a scheduled task comes through and processes those at a later time (but perhaps not all at once) and that might be a way of using less memory. Perhaps there’s data that is being stored in vars that isn’t needed. Maybe you only need certain things to store and other things can be called as needed instead of stored later. However, if we don’t know what you’re doing, it’s hard for us to tell you if there’s a way to optimize it.

Start working your way through the code manually and monitor the memory usage. Some general principals might include:

  • Only get data that you need. If you’re getting all AD Computers, and only need a couple properties. only get those properties. Don’t bother getting all the properties if you are having memory issues.
  • Use the pipeline. In some cases you may not need to store something to loop through it. A lot of people do for various reasons, but sometimes using the pipeline operator against a command might help with RAM issues depending on the situation.
  • Avoid using the increase assignment operator if using it.
  • Look into Garbage Collection: [System.gc]::Collect()
  • Avoid additional calls. Sometimes the data you need is actually in the property of something you already have.
  • If using third party modules in your script, look to see if there’s ways to gain advantages there. For example, There was a recent article on the Exchange Online module where the MS devs posted about ways to help save memory as that module used more memory than previous modules. They have specific tips for their module to help decrease memory usage.

There’s probably a lot more out there on Google but those are just some that I could think of.