If you have not done so yet, I recommend studying this blog article here:
https://www.sapien.com/blog/2022/06/14/ ... e-project/
By the nature of a service, stopping a service does terminate the process. If it does not, something is not handled correctly. It sends a stop service event to the service. Which is what the associated handler is supposed to, well, handle. You must release any objects that you specifically allocated here and make sure to set the proper flags for the main service loop to exit.
Resources *should* be allocated at the start-service and all resources must be released at the stop-service event. This includes any resources allocated and not released during the normal operation of the service. It seems that is not what you do. Likewise, the pause-service event could release accumulated resources and re-allocated them as needed, but that is an optional thing. It does however have to suspend the services processing loop.
PowerShell itself was not designed to create long running applications. The memory model is scope based, so if something does not go out of scope it is never released. Assigning a new object to the same global scope variable will inevitable create memory leaks.
I would recommend to test and debug the code making up the body of your service (Invoke-MyService) in a separate non-service project for easier debugging and resetting. If you use third party modules or elements that leak memory, it might be a good idea to relocate them to a separate process that can be started and stopped from the service when needed.
The hosting shell around a PowerShell service does not execute additional processes nor does it allocate any resources other than a PowerShell engine during startup. Specially not during ongoing operation. So any leaking memory is coming from your code in one fashion or another.
https://www.sapien.com/blog/2022/06/14/ ... e-project/
By the nature of a service, stopping a service does terminate the process. If it does not, something is not handled correctly. It sends a stop service event to the service. Which is what the associated handler is supposed to, well, handle. You must release any objects that you specifically allocated here and make sure to set the proper flags for the main service loop to exit.
Resources *should* be allocated at the start-service and all resources must be released at the stop-service event. This includes any resources allocated and not released during the normal operation of the service. It seems that is not what you do. Likewise, the pause-service event could release accumulated resources and re-allocated them as needed, but that is an optional thing. It does however have to suspend the services processing loop.
PowerShell itself was not designed to create long running applications. The memory model is scope based, so if something does not go out of scope it is never released. Assigning a new object to the same global scope variable will inevitable create memory leaks.
I would recommend to test and debug the code making up the body of your service (Invoke-MyService) in a separate non-service project for easier debugging and resetting. If you use third party modules or elements that leak memory, it might be a good idea to relocate them to a separate process that can be started and stopped from the service when needed.
The hosting shell around a PowerShell service does not execute additional processes nor does it allocate any resources other than a PowerShell engine during startup. Specially not during ongoing operation. So any leaking memory is coming from your code in one fashion or another.
Statistics: Posted by Alexander Riedel — Fri Oct 11, 2024 11:59 pm