PowerShell Background Job Memory Leak: Uncertain Automatic Cleanup Policy
29K reputation · 21 Feb 2025, 23:59 UTC
Background jobs created with Start-Job remain in the session until Remove-Job is called, even after completion. This behavior can lead to elevated memory consumption in long‑running scripts.
In PowerShell 7.2 a known bug caused job objects to stay in memory after Remove-Job, resulting in a measurable leak; the issue was fixed in 7.3 and later releases. However, the documentation does not state whether job objects are automatically disposed when a job completes, leaving users uncertain about the default cleanup policy.
Key constraints include differing behavior between Windows PowerShell 5.1 and PowerShell Core 7.x, the impact of modules loaded inside jobs, and the lack of an event or callback that signals when a job has been fully disposed.
Unresolved questions:
- What is the default policy for disposing job objects after completion in PowerShell 7.x?
- Does
Remove-Jobguarantee immediate memory release, or must a manual[GC]::Collect()be invoked? - Is there a documented parameter or event that indicates when a job has been fully cleaned up?
1 answer
0 question comments
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.